Dotnet_practice questions
Dotnet_practice questions
Module Program
Sub Main()
Dim p As New Person()
p.Name = "Alice"
p.Age = 25
p.SayHello()
Console.ReadLine()
End Sub
End Module
2️⃣ Constructors
' Constructor
Public Sub New(t As String, a As String)
Title = t
Author = a
End Sub
Module Program
Sub Main()
Dim b As New Book("1984", "George Orwell")
b.ShowDetails()
Console.ReadLine()
End Sub
End Module
3️⃣ Inheritance
Module Program
Sub Main()
Dim d As New Dog()
d.Speak()
d.Bark()
Console.ReadLine()
End Sub
End Module
4️⃣ Polymorphism
Module Program
Sub Main()
Dim animals As List(Of Animal) = New List(Of Animal) From {
New Cat(),
New Dog()
}
Console.ReadLine()
End Sub
End Module
5️⃣ Interfaces
Module Program
Sub Main()
Dim c As New Car()
c.Start()
c.StopDriving()
Console.ReadLine()
End Sub
End Module
Module Program
Sub Main()
Dim result As Integer = MathHelper.Add(5, 3)
Console.WriteLine("Result: " & result)
Console.ReadLine()
End Sub
End Module
7. Exception handling
Imports System
Module Program
Sub Main()
Try
Console.WriteLine("Enter a number:")
Dim num1 As Integer = Convert.ToInt32(Console.ReadLine())
Console.WriteLine("Enter another number:")
Dim num2 As Integer = Convert.ToInt32(Console.ReadLine())
Dim result As Integer = Divide(num1, num2)
Console.WriteLine("Result of division: " & result)
Catch ex As FormatException
Console.WriteLine("Error: Please enter only numeric values.")
Catch ex As DivideByZeroException
Console.WriteLine("Error: Division by zero is not allowed.")
Catch ex As Exception
Console.WriteLine("General Error: " & ex.Message)
Finally
Console.WriteLine("This block always executes (Finally).")
End Try
Console.WriteLine("Program continues...")
Console.ReadLine()
End Sub
' Function that throws exception if divisor is zero
Function Divide(a As Integer, b As Integer) As Integer
If b = 0 Then
Throw New DivideByZeroException("Cannot divide by zero.")
End If
Return a \ b ' Integer division
End Function
End Module
8. Programme to illustrate use of Collections.
Imports System.Collections
Imports System.Collections.Generic
Module Program
Sub Main()
' 1. ArrayList (non-generic - allows different types)
Console.WriteLine("---- ArrayList Example ----")
Dim arrList As New ArrayList()
arrList.Add("Apple")
arrList.Add(123)
arrList.Add(True)
Console.ReadLine()
End Sub
End Module
---- ArrayList Example ----
Apple
123
True