You are on page 1of 15

Visual Programming Name : _____________________ Max.

Marks: 10
Course Code: SE-303 Roll No: _____________________ Time: 20 Minutes Cutting,
overwriting, Erasing, Fluid Painting and use of Lead Pencil will earn no marks. Attempt all
questions of the objective portion ON the question paper.
OBJECTIVE
Question 1: Encircle the right options from the following statements.
Marks]

[10x01 = 10

Which of the following job is performed by Garbage Collector?


Freeing memory on the stack.
Freeing memory occupied by unreferenced objects.
Closing unclosed database collections.
Closing unclosed files.

Which of following is NOT a value type?


Integer
Array
Structure
Long

Which of the following statements is correct about the C#.NET code snippet given below?
short s1 = 200;
short s2 = 400;

int a = s1 * s2;

A value 80000 will be assigned to a.


A negative value will be assigned to a.
During arithmetic if the result exceeds the high or low value of the range the value wraps
around till the other side of the range.
An error is reported as widening conversion cannot takes place.
An overflow error will be reported since the result of the multiplication exceeds the range of a
Short Integer.

How many times can a constructor be called during lifetime of the object?
As many times as we call it.
Only once.
Depends upon a Project Setting made in Visual Studio.NET.
Any number of times before the object gets garbage collected.
Any number of times before the object is deleted.

Which of the following statements are correct about the C#.NET code snippet given below?
int[] a = { 11, 3, 5, 9, 4 };

The array elements are created on the stack.


Refernce a is created on the stack.
Whether the array elements are stored in the stack or heap depends upon the size of the array.
The system will generate a compile time error as the syntax of declaring array is not correct.

How will you complete the foreach loop in the C#.NET code snippet given below such that it
correctly iterate all elements of the array a?
int[] a = new int[30];

foreach (int j = 1; j < a(0).GetUpperBound; j++)


foreach (int j = 1; j < a.GetUpperBound (0); j++)
foreach (int j in a.Length)
foreach (int j in a)
foreach (int j in a.Length -1)

If Sample class has a Length property with get accessor then which of the following statements
will work correctly?

Sample m = new Sample();


m.Length = 10;

Sample m = new Sample();


m.Length = m.Length + 20;

Sample m = new Sample();


int length = m.Length;

Sample.Length = 20;

Console.WriteLine(Sample.Length);

Which of the following will be the correct result of the statement b = a in the C#.NET code
snippet given below?;

struct Address
{
private int street;
private String city;
}
Address a = new Address();
Address b = a;

All elements of a will get copied into corresponding elements of b.


Address stored in a will get copied into b.
Once assignment is over a will get garbage collected.
Once assignment is over a will go out of scope, hence will die.
Address of the first element of a will get copied into b.

When would a struct variable get destroyed?


When no reference refers to it, it will get garbage collected.
Depends upon whether it is created using new or without using new.
When it goes out of scope.
Depends upon the Project Settings made in Visual Studio.NET.

Depends upon whether we free it's memory using free() or delete().

Which of the following CANNOT be used as an underlying datatype for an enum in C#.NET?
byte
short
float
int

Visual Programming Name : _____________________ Max. Marks: 15 Course Code: SE303 Roll No: _____________________ Time: 70 Minutes SUBJECTIVE-SECTION:
Note: Attempt all questions on answer sheet. Paper interpretation itself is the part of paper, so
no query will be entertained during the exam.

Question 2:
[3 Marks]
Develop a class called Date with the following members and methods:
private data members of type int: day, month, year
properties for each private data member
methods:
2 constructors:
a default constructor that sets the date to October 1, 1979
an overloaded constructor that takes 3 arguments, and assigns them to the day, month
and year data members

A set date method, which takes 3 arguments, day, month and year and assigns them to the
data members
A print method that prints the date in traditional format "dd/mm/yyyy"

Solution:
class Date
{
private int day;
private int month;
private int year;

public int Day


{
get { return day; }
set { day = value; }
}

public int Month


{
get { return month; }
set { month = value; }
}

public int Year


{
get { return year; }
set { year = value; }
}

public Date()
{
this.day = 1;
this.month = 9;

// 1st
// October (zero based index)

this.year = 1979; // 1979


}

public Date(int day, int month, int year)


{
this.day = day;
this.month = month;
this.year = year;
}

public void SetDate(int day, int month, int year)


{
this.day = day;
this.month = month;

this.year = year;
}

public void PrintDate()


{
Console.WriteLine("{0:00}/{1:00}/{2:0000}", day, month, year);
}

Question 3:
[2 Marks]
Create an enum Gender with values (Male, Female)
Create a struct Person with following members and methods:
private data members: id of type int, name of type string, gender of type Gender
a constructor that takes 3 arguments, and assigns them to the id, name and gender data
members

Solution:
enum Gender
{
Male,
Female
}

struct Person
{
private int id;
private String name;
private Gender gender;

public Person(int id, String name, Gender gender)


{
this.id = id;
this.name = name;
this.gender = gender;
}
}

Question 4:
[2 Marks]
Create a DataTable of name "Student" with following columns RollNumber, Name, Section
Add two rows in Student DataTable with following values
1, Asad, A
2, Qaisar, B

Solution:

DataTable student = new DataTable();

DataColumn rollNumber = new DataColumn("RollNumber");


DataColumn name = new DataColumn("Name");
DataColumn section = new DataColumn("Section");

student.Columns.Add(rollNumber);
student.Columns.Add(name);
student.Columns.Add(section);

DataRow dataRow1 = student.NewRow();


dataRow1["RollNumber"] = "1";
dataRow1["Name"] = "Asad";
dataRow1["Section"] = "A";
student.Rows.Add(dataRow1);

DataRow dataRow2 = student.NewRow();


dataRow2["RollNumber"] = "2";
dataRow2["Name"] = "Qaisar";
dataRow2["Section"] = "B";
student.Rows.Add(dataRow2);

Question 5:
[3 Marks]
Create a function for database connectivity, the function will perform the followings

- Open connection by using a connection-string (use a dummy value)


- Execute command to get record-set (use reader method)
- Loop through the reader and print rows data
- Close reader
- Close connection

You can use the query "SELECT ID, NAME, EMAIL FROM STUDENT"; assume all fields are
String types.

Solution:

/// create connection object


OracleConnection connection = new OracleConnection();

/// set connection string of the connection


connection.ConnectionString = "<connection string>";

/// open connection


connection.Open();

/// create command with query string and opened connection


OracleCommand command = new OracleCommand(QUERY, connection);

/// execute reader to get data reader (open cursor)


OracleDataReader reader = command.ExecuteReader();

/// iterate over reader


while (reader.Read())
{
/// print data by reading through data reader, one record per iteration
Console.WriteLine("{0}\t{1}\t{2}\t{3}", reader.GetInt32(0), reader.GetString(1),
reader.GetString(2), reader.GetString(3));
}

/// close reader explicitly, this step is compulsory in order to free the connection object.
reader.Close();

/// close connection explicitly, this step is also necessary.


connection.Close();

Question 6:
[5 Marks]
Create a program which will
1- create a Rectangular array
2- copy contents of the passed Jagged array into a Rectangular array
3- return back the Rectangular array

The Rectangular array will be created in such a way


- Rectangular array will have the number of rows equal to the Jagged array
- Rectangular array will have the number of columns equal to the maximum column length of a
Jagged array
- The extra elements of the Rectangular array in each row will be padded with zero (0) value, if
the the row size is greater than the Jagged array row size

int[,] ConvertArray(int[][] jArray)


{
int[,] rArray = null;
// your code will be here

return rArray;
}

Hint: First, you need to compute the maximum column length of the Jagged array and then
initialize the rArray

Example,
Jagged array (input)

587

63829 17

Rectangular array (output)

58700 63829 17000

Solution:

int[,] ConvertArray(int[][] jArray)


{
int[,] rArray = null;

// compute max column width


int colWidth = 0;
for (int i = 0; i < jArray.Length; i++)
{
if (jArray[i].Length > colWidth)
{
colWidth = jArray[i].Length;
}
}

// create rectangullar array


rArray = new int[jArray.Length, colWidth];

// copy array contents


for (int i = 0; i < jArray.Length; i++)
{

for (int j = 0; j < jArray[i].Length; j++)


{
rArray[i, j] = jArray[i][j];
}
}

return rArray;
}

-----------------------------------------------------------G((d
Luck-----------------------------------------------------------

You might also like