VB.Net find output programs (Data Types) | set 4

Find the output of VB.Net programs | Data Types | Set 4: Practice these programs to test and enhance the knowledge of VB.Net Data Types.
Submitted by Nidhi, on June 15, 2022

Question 1:

Module VBModule
 
    Sub Main()
        dim num1 as Single = 10.23
        dim num2 as Double = 10.45
        dim num3 as UShort =  25
        
        Console.WriteLine("Type of num1: {0}",num1.GetType())
        Console.WriteLine("Type of num2: {0}",num2.GetType())
        Console.WriteLine("Type of num3: {0}",num3.GetType())
    End Sub
    
End Module

Output:

Type of num1: System.Single
Type of num2: System.Double
Type of num3: System.UInt16

Explanation:

In the above program, we created 3 variables. Then we used GetType() method the type of the variable and printed the result.


Question 2:

Module VBModule
 
    Sub Main()
        dim num as Single = 10.23
        
        if(num.GetType() = "System.Single")
            Console.WriteLine("Hello")
        else
            Console.WriteLine("Hiii")
        end if
        
    End Sub
    
End Module

Output:

main.vb (6,44) : error VBNC30518: CHANGEME

There were 1 errors and 0 warnings.

Explanation:

The above program will generate a syntax error because GetType() method does not return the string, but we are comparing its result with the string "System.Single". So here we need to convert the result of the GetType() method into the string using the ToString() method.


Question 3:

Module VBModule
 
    Sub Main()
        dim num as Single = 10.23
        
        if(num.GetType().ToString = "System.Single")
            Console.WriteLine("Hello")
        else
            Console.WriteLine("Hiii")
        end if
        
    End Sub
    
End Module

Output:

Hello

Explanation:

In the above program, we created a variable num of type Single. Then we used GetType() method to get its type and then we convert the result of the GetType() method into the string using the ToString() method. So the "if" condition gets true and printed "Hello" message.


Question 4:

Module VBModule
 
    Sub Main()
        dim num1 as SByte = 12
        dim num2 as SByte = 20
        dim num3 as Byte  = 0 
        
        num3 = num1*num2
        
        Console.WriteLine("Result is: "&num3)
        
    End Sub
    
End Module

Output:

Result is: 240

Explanation:

In the above program, we created 2 variables num1, num2, num3 that are initialized with 12, 20, 0 respectively. Then we multiply the values of num1 and num2 and assigned the result to the num3 variable. After that, we printed the result.


Question 5:

Module VBModule
 
    Sub Main()
        dim num1 as SByte = 12
        dim num2 as SByte = 20
        dim num3 as Integer  = 0 
        
        num3 = len(num1)*len(num2)/ len(num1.GetType().ToString)
        
        Console.WriteLine("Result is: "&num3)
        
    End Sub
    
End Module

Output:

Result is: 0

Explanation:

In the above program, we created 2 variables num1, num2, num3 that are initialized with 12, 20, 0.

Now we evaluate the expression,

num3 = len(num1)*len(num2)/ len(num1.GetType().ToString)
num3 = 1*1/len(“System.SByte”)
num3 = 1/12
num3 = 0





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.