0% found this document useful (0 votes)
4 views

Dotnet_practice questions

The document provides an overview of various programming concepts in Visual Basic, including classes, constructors, inheritance, polymorphism, interfaces, static classes, exception handling, and collections. Each concept is illustrated with code examples demonstrating their usage. The document also showcases the functionality of ArrayLists, Lists, and Dictionaries.

Uploaded by

prathamy831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Dotnet_practice questions

The document provides an overview of various programming concepts in Visual Basic, including classes, constructors, inheritance, polymorphism, interfaces, static classes, exception handling, and collections. Each concept is illustrated with code examples demonstrating their usage. The document also showcases the functionality of ArrayLists, Lists, and Dictionaries.

Uploaded by

prathamy831
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Objects and Classes

Public Class Person


Public Name As String
Public Age As Integer

Public Sub SayHello()


Console.WriteLine("Hello, my name is " & Name & " and I am " & Age
& " years old.")
End Sub
End Class

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

Public Class Book


Public Title As String
Public Author As String

' Constructor
Public Sub New(t As String, a As String)
Title = t
Author = a
End Sub

Public Sub ShowDetails()


Console.WriteLine("Title: " & Title & ", Author: " & Author)
End Sub
End Class

Module Program
Sub Main()
Dim b As New Book("1984", "George Orwell")
b.ShowDetails()
Console.ReadLine()
End Sub
End Module

3️⃣ Inheritance

Public Class Animal


Public Sub Speak()
Console.WriteLine("The animal makes a sound.")
End Sub
End Class

Public Class Dog


Inherits Animal

Public Sub Bark()


Console.WriteLine("The dog barks.")
End Sub
End Class

Module Program
Sub Main()
Dim d As New Dog()
d.Speak()
d.Bark()
Console.ReadLine()
End Sub
End Module

4️⃣ Polymorphism

Public Class Animal


Public Overridable Sub Speak()
Console.WriteLine("Animal speaks.")
End Sub
End Class

Public Class Cat


Inherits Animal

Public Overrides Sub Speak()


Console.WriteLine("Cat meows.")
End Sub
End Class

Public Class Dog


Inherits Animal

Public Overrides Sub Speak()


Console.WriteLine("Dog barks.")
End Sub
End Class

Module Program
Sub Main()
Dim animals As List(Of Animal) = New List(Of Animal) From {
New Cat(),
New Dog()
}

For Each a As Animal In animals


a.Speak()
Next

Console.ReadLine()
End Sub
End Module

5️⃣ Interfaces

Public Interface IDrive


Sub Start()
Sub StopDriving()
End Interface

Public Class Car


Implements IDrive

Public Sub Start() Implements IDrive.Start


Console.WriteLine("Car is starting.")
End Sub

Public Sub StopDriving() Implements IDrive.StopDriving


Console.WriteLine("Car has stopped.")
End Sub
End Class

Module Program
Sub Main()
Dim c As New Car()
c.Start()
c.StopDriving()
Console.ReadLine()
End Sub
End Module

6️⃣ Static Class (Shared in )

Public NotInheritable Class MathHelper


' Shared method
Public Shared Function Add(a As Integer, b As Integer) As Integer
Return a + b
End Function
End Class

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)

For Each item In arrList


Console.WriteLine(item)
Next

' 2. List(Of T) (generic - type-safe)


Console.WriteLine(vbCrLf & "---- List(Of String) Example ----")
Dim fruits As New List(Of String) From {"Mango", "Banana",
"Pineapple"}
fruits.Add("Orange")
fruits.Remove("Banana")

For Each fruit In fruits


Console.WriteLine(fruit)
Next

' 3. Dictionary(Of TKey, TValue)


Console.WriteLine(vbCrLf & "---- Dictionary Example ----")
Dim studentMarks As New Dictionary(Of String, Integer)()
studentMarks("Alice") = 85
studentMarks("Bob") = 90
studentMarks("Charlie") = 78

For Each kvp As KeyValuePair(Of String, Integer) In studentMarks


Console.WriteLine(kvp.Key & " scored " & kvp.Value)
Next

Console.ReadLine()
End Sub
End Module
---- ArrayList Example ----
Apple
123
True

---- List(Of String) Example ----


Mango
Pineapple
Orange

---- Dictionary Example ----


Alice scored 85
Bob scored 90
Charlie scored 78

You might also like