Visual Programming Paper 2021
Visual Programming Paper 2021
Section-A
Q.1 Differentiate between multidimensional array and jagged array.
Both multidimensional arrays and jagged arrays are used to store data in memory, but they have some
fundamental differences.
A multidimensional array is an array that has more than one dimension. It is a grid-like structure where
data is stored in rows and columns, similar to a spreadsheet. Each element in the array is accessed using
two or more indices, corresponding to the coordinates of the element in the grid. For example, a 2-
dimensional array can be thought of as a table with rows and columns, while a 3-dimensional array can
be thought of as a cube with rows, columns, and layers.
In contrast, a jagged array is an array of arrays, where each element in the array is itself an array. The
arrays within the jagged array can have different sizes and dimensions, giving it a jagged or irregular
shape. To access an element in a jagged array, you need to first index the outer array to get the inner
array, and then index the inner array to get the element itself.
Multidimensional array:
Jagged array:
As you can see, a multidimensional array is created using a single statement and all the elements are
initialized to default values. On the other hand, a jagged array is created by creating individual arrays
and then adding them to the outer array. Each inner array can have a different size and shape.
Q.2 What is multiple inheritance? What is the main problem with this approach in C#?
Multiple inheritance is a feature in object-oriented programming (OOP) that allows a class to inherit
from more than one base class. This means that the derived class will have access to the members and
methods of all of its base classes. In C#, however, multiple inheritance is not supported directly. C# only
supports single inheritance, where a class can inherit from only one base class. The main reason for this
is to avoid the diamond problem, which is a common issue that arises in languages that support multiple
inheritance.
The diamond problem occurs when a class inherits from two or more base classes that have a common
parent class. This creates ambiguity when a derived class tries to access a member or method of the
common parent class through one of its base classes. It becomes unclear which implementation of the
member or method should be used.
To avoid this problem, C# uses interfaces, which are similar to classes but only contain method
signatures and constants. A class can implement multiple interfaces, which allows it to define behavior
that is not tied to a specific class hierarchy.
In summary, multiple inheritance is a feature in OOP that allows a class to inherit from more than one
base class, but it is not supported in C# directly due to the diamond problem. Instead, C# uses interfaces
to achieve similar functionality without introducing ambiguity.
Q.3 Declare and initialize an Array and List as well with the following values:
10,20,30,40,50,60,70,80,90,100
Here's how you can declare and initialize an array and a list with the given values in C#:
// Array
int[] myArray = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
// List
List<int> myList = new List<int> { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
myList.Add(110);
myList.Remove(30);
myList.Sort();
You can use these operations to modify the list as needed. Note that arrays have a fixed size, so you
cannot add or remove items from them like you can with lists.
Q.4 Suppose Marks is an array having sum number. The below query will execute or not, first just
answer "Yes" or "No", if your answer is yes, what value will be store in sort, if your answer is NO,
write the correct syntax of the query.
static void Main(string[] args)
No, the query will not execute because it has a syntax error. The correct syntax to select values from an
array using LINQ in C# would be:
select val;
This query will select all values from the Marks array that are greater than 68, and store them in the sort
variable. Note that the array values must be enclosed in curly braces { } to declare and initialize the array
correctly.
Q.5 For the below client code, write for statement and foreach statement do display the list value.
Note: write the output in correct format.
names.Add("amir");
names.Add("kk");
names.Insert(0, "khattak");
Here is an example of how you can display the values in the names list using a for loop and a foreach
loop in C#:
names.Add("amir");
names.Add("kk");
names.Insert(0, "khattak");
Console.WriteLine(names[i]);
Console.WriteLine(name);
khattak
amir
kk
khattak
amir
kk
Note that the Insert method is used to insert the value "khattak" at the beginning of the list, so it will be
displayed first when iterating over the list using the loops.
Section-C
Q.1 Write the LINQ query to achieve the following
a. Find all items starting with letter "R" in the following code then print the result b. Find all
items.Add("Aqua");
items.Add("Rust");
items.Add("Yellow");
items.Add("Red");
items.Add("Aqua");
items.Add("Rust");
items.Add("Yellow");
items.Add("Red");
// a. Find all items starting with letter "R" and print the result
Console.WriteLine(item);
// b. Find all items with length less than 5 and print them
Console.WriteLine(item);
}
Q.2 Textbox is an important control used in C# GUI environment. You are required to implement the
validation "this textbox can only accept numeric values". Create a suitable event handler and write
code to implement this validation.
To implement the validation "this textbox can only accept numeric values" in C#, we can use the
KeyPress event of the TextBox control. The KeyPress event is raised when the user presses a key while
the TextBox has focus.
In this code, we first check if the pressed key is a control character (such as backspace) or a digit. If it is
not a control character or a digit, we set the Handled property of the KeyPressEventArgs to true, which
ignores the input and prevents it from being added to the TextBox.
To use this code, you need to attach the textBox1_KeyPress event handler to the KeyPress event of the
TextBox control. You can do this either in the designer by selecting the TextBox control and opening the
Properties window, or in code by adding the following line of code to the constructor or initialization
method of the form:
Here textBox1 is the name of the TextBox control, and textBox1_KeyPress is the name of the event
handler method.