0% found this document useful (0 votes)
18 views6 pages

Visual Programming Paper 2021

Uploaded by

Sana Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
18 views6 pages

Visual Programming Paper 2021

Uploaded by

Sana Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

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.

Here's an example to illustrate the difference:

Multidimensional array:

int[,] grid = new int[3,3]; // creates a 3x3 grid

grid[0,0] = 1; // set element at row 0, column 0 to 1

grid[0,1] = 2; // set element at row 0, column 1 to 2

Jagged array:

int[][] jagged = new int[3][];

jagged[0] = new int[2] { 1, 2 }; // create a 1x2 array at index 0

jagged[1] = new int[3] { 3, 4, 5 }; // create a 1x3 array at index 1

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

Use at least three operations for List.

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 };

Here are three operations you can perform on the list:

Add an item to the list:

myList.Add(110);

Remove an item from the list:

myList.Remove(30);

Sort the list in descending order:

myList.Sort();

myList.Reverse(); // Reverses the order to get descending order

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)

int[] Marks = 50, 60, 70, 80, 90 };

var sort select val

where val > 68 from val in Marks;

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:

int[] Marks = { 50, 60, 70, 80, 90 };

var sort = from val in Marks

where val > 68

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.

static void Main(string[] args)

List names new List();

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#:

// Declare and initialize the list


List<string> names = new List<string>();

names.Add("amir");

names.Add("kk");

names.Insert(0, "khattak");

// Display the values in the list using a for loop

Console.WriteLine("Values in the list using a for loop:");

for (int i = 0; i < names.Count; i++)

Console.WriteLine(names[i]);

// Display the values in the list using a foreach loop

Console.WriteLine("\nValues in the list using a foreach loop:");

foreach (string name in names)

Console.WriteLine(name);

The OutPut of this will be

Values in the list using a for loop:

khattak

amir

kk

Values in the list using a foreach loop:

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 with length less than 5 and print them

List<string> items new List<string>();

items.Add("Aqua");

items.Add("Rust");

items.Add("Yellow");

items.Add("Red");

List<string> items = new List<string>();

items.Add("Aqua");

items.Add("Rust");

items.Add("Yellow");

items.Add("Red");

// a. Find all items starting with letter "R" and print the result

var itemsStartingWithR = items.Where(item => item.StartsWith("R"));

foreach (var item in itemsStartingWithR)

Console.WriteLine(item);

// b. Find all items with length less than 5 and print them

var itemsWithLengthLessThan5 = items.Where(item => item.Length < 5);

foreach (var item in itemsWithLengthLessThan5)

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.

Here is an example code that implements this validation:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))

e.Handled = true; // ignore the input

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:

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);

Here textBox1 is the name of the TextBox control, and textBox1_KeyPress is the name of the event
handler method.

You might also like