Home »
VB.Net »
VB.Net find output programs
VB.Net find output programs (Data Types) | set 1
By Nidhi Last Updated : November 14, 2024
Question 1:
Module VBModule
Sub Main()
dim num1 as integer=10
dim num2 as Double =10.24
Console.WriteLine("Num1: "+num1)
Console.WriteLine("Num2: "+num2)
End Sub
End Module
Output:
Unhandled Exception:
System.InvalidCastException: Conversion from string "Num1: " to type 'Double' is not valid. ---> System.FormatException: Input string was not in a correct format.
Explanation:
The above program will generate an exception due to the below statements.
Console.WriteLine("Num1: "+num1)
Console.WriteLine("Num2: "+num2)
In Vb.net, we need to use the "&" operator for concatenation. The correct program is given below.
Module VBModule
Sub Main()
dim num1 as integer=10
dim num2 as Double =10.24
Console.WriteLine("Num1: "&num1)
Console.WriteLine("Num2: "&num2)
End Sub
End Module
Question 2:
Module VBModule
Sub Main()
dim num1 as integer=10
dim num2 as Double =10.24
Console.WriteLine("Num1: {0}",num1)
Console.WriteLine("Num2: {1}",num2)
End Sub
End Module
Output:
Num1: 10
Num2: 10.24
Explanation:
In the above program, we created 2 variables and printed them using Console.WriteLine() method.
Question 3:
Module VBModule
Sub Main()
dim num as integer=10
dim length as integer=0
length=sizeof(num)
Console.WriteLine("Num: {0}",num)
Console.WriteLine("Length: {0}",length)
End Sub
End Module
Output:
error VBNC30451: 'sizeof' is not declared. It may be inaccessible due to its protection level.
There were 1 errors and 0 warnings.
Explanation:
The above program will generate a syntax error because the "sizeof" operator is not available in vb.net.
Question 4:
Module VBModule
Sub Main()
dim num1 as int=10
dim num2 as Double =10.24
Console.WriteLine("Num1: "&num1)
Console.WriteLine("Num2: "&num2)
End Sub
End Module
Output:
main.vb (4,24) : error VBNC30451: 'int' is not declared. It may be inaccessible due to its protection level.
There were 1 errors and 0 warnings.
Explanation:
The above program will generate a syntax error because int is not a built-in type in vb.net, we need to use integer. The correct program is given below,
Module VBModule
Sub Main()
dim num1 as integer=10
dim num2 as Double =10.24
Console.WriteLine("Num1: "&num1)
Console.WriteLine("Num2: "&num2)
End Sub
End Module
Question 5:
Module VBModule
Sub Main()
dim num1=10 as integer
dim num2=10.24 as Double
Console.WriteLine("Num1: {0}",num1)
Console.WriteLine("Num2: {0}",num2)
End Sub
End Module
Output:
main.vb (4,23) : error VBNC30205: End of statement expected.
main.vb (4,23) : error VBNC90019: Expected 'End'.
main.vb (5,26) : error VBNC90018: Expected end of line.
main.vb (7,16) : error VBNC90019: Expected 'End'.
main.vb (8,16) : error VBNC30203: Identifier expected.
main.vb (9,12) : error VBNC30203: Identifier expected.
main.vb (11,11) : error VBNC30203: Identifier expected.
There were 7 errors and 0 warnings.
Explanation:
The above program will generate syntax errors, because of the below declarative statements,
dim num1=10 as integer
dim num2=10.24 as Double
The correct program is given below,
Module VBModule
Sub Main()
dim num1 as integer=10
dim num2 as Double=10.24
Console.WriteLine("Num1: {0}",num1)
Console.WriteLine("Num2: {0}",num2)
End Sub
End Module