You are on page 1of 8

Faculty of Science & Engineering Stage: 2th year

School of Science Date: / /2014


Department of Computer Science Time: 2 hours
Subject: Object Oriented Programming (OOP)

Final Examination (2013-2014) First Semester ( ) Attempt

Part A: Multiple choices [40 points]


Each question has 5 marks
1- Which one of the following lines is not a correct comment?
a. //This is/*my comment/**
b. /* This is // my *///comment
c. //This is my comment*/
d. /* This is my //comment/*

2- How many times will the word “Zanko” be printed in the following code?

for (int i = 0; i <= 10; i++)


{
if (i == 5)
i -= 2;
if (i == 5)
break;
Console.WriteLine("Zanko");
i++;
}

a. 5 b. infinite c. 7 d. 6

3- Which looping mechanism executes at least once?

a. if …. else b. while c. do…..while d. for

Head of Department 1 Examiner


4- What is the value of b[a[1][2] + b[4]] when we consider the following two
arrays:
int[][] a = new int[3][];
int[] b = { 1, 2, 0, 4, 1, 3};
a[0] = new int[] { 2, 3 };
a[1] = new int[] { 1, 2, 4, 5 };
a[2] = new int[] { 1, 0 };

a. 4 b. 3 c. 0 d. 2

5- What will be printed on the screen in the following code?


class Fold
{
public void mutilate(int[] a, int b, int c)
{
a[0] = 7;
b = 8;
c = 9;
}
}
class Program
{
static void Main(string[] args)
{
Fold ob = new Fold();

int[] a = new int[] {1,2,3,4};


int b = 5;
int c =6;
ob.mutilate( a , b, c);
Console.Write( a[0]+""+ b + c);
}
}

a. 156 b. 711 c. 756 d.789

6- What is the output of the following code fragment?

int[] a = new int[4];


for (int i = 3; i > 0; i--)
a[i] = a[i - 1] + i;
Console.WriteLine(a[2]);

a. 1 b. 2 c. 3 d. stuck in loop, it prints nothing

Head of Department 2 Examiner


7- Which keyword specifies that an object or variable is not modifiable after it
has been initialized at execution time.

a. constant b. readonly c. static d. garbage collection

8- What will be printed on the screen after running the following code?

public class Base


{
int i;
static int j;
public Base(int a, int b)
{
i = a;
j = b;
Console.Write(2*Show());
}
public Base()
{
i = 6;
j = 3;
Console.Write(2 * Show());
}
public int Show()
{
return i/j;
}
~Base()
{
Console.Write(Show());
}
}
namespace Exam_Question2
{
class Program
{
static void Main(string[] args)
{
Base ob1 = new Base();
Base ob2 = new Base(12, 4);
ob2 = null;
ob1 = null;
}
}
}

a. Compile error b. 4632 c. 4631 d. 4613

Head of Department 3 Examiner


Part B: Be a compiler (Error detection and correction) [15
points]
In the following codes, determine the errors and then resolve them. If a code has no error,
mention that the code has no problem and leave it unchanged. (Each question has 5 points.)

9-
using System;
public class Example
{
static int a;
private int b;
const int c;
public int getItem(int i, int j)
{
a = i;
b= j;
c = i + j;

}
public Example(int d)
{
a = b = 5;
}
~Example(int k)
{
a = b = k;
}
}
namespace Exam_Question2
{
class Program
{
static void Main(string[] args)
{
Example ob = new Example();
ob.getItem(4, 5);
ob.a = 6;
}
}
}

Head of Department 4 Examiner


10-
static void Main(string[] args)
{
int[] a = new int[20];
int b;
int[,] b = { { 1, 2 }, { 3, 4, 0 } };
int[][] c = new int[][];
c[0] = new int { 0, 0, 1 };
c[1] = new int { 0, 0 };

for (int i = 0; i <= 20; i++)


a[i] = 0;
foreach(b in a)
{
if(b>1)
Console.Write(b);
}
}

11-
using System;
public class Base
{
int sum(int x, y)
{
int result;
result = x + y;
}

int g() {
Console.WriteLine( "This is for test" );
int h() {
Console.WriteLine( "I will kill you if you don't find this
error" );
}
}
}
namespace Exam
{
class Program
{
static void Main(string[] args)
{
Base ob = new Base;
int a = ob.sum + ob.g();
}
}
}

Head of Department 5 Examiner


Part C: Analyzing programs [25 points]
If you think a program has an error(s), indicate that the program has error(s) and state what the
error(s) are. (Each question has 4 points)

12- What is the output of the following code?


int i = 0, j = 5 ;
for( ; (i < 3) && (j++ < 10) ; i++ ) {
Console.Write(" " + i + " " + j );
}
Console.Write(" " + i + " " + j );

13- What will be printed by running the following code?


class Program
{
static void Main(string[] args)
{
int num1 = 1;
int num2 = 2;
do_calc(num2, ref num1);
Console.WriteLine(num2 + num1);

}
static int do_calc(int n1, ref int n2)
{
n1 = n1 + 5;
n2 = n2 * 5;
Console.WriteLine(n1+ n2);
return n1;
}

14- Consider the following array: a[] = {3, -1, -6, 2}. What is the value of
a[a[3] - a[1]]?

Head of Department 6 Examiner


15- What is the output of the following code?
class Program
{
static void Main(string[] args)
{
int i,j;
for (i = 0, j = 10; i <= 10 && j >= 0; i++)
{
if (i == 3 || j == 4)
continue;
Set(ref j);
Console.Write(i + j);
j--;
}
}
static int Set(ref int k)
{
k -= 2;
return k+5;
}
}

16- What is the output of the following line?


Console.WriteLine("\n*\n**\n***\r****");

Part E: Writing programs [20 points]

17- Create a class Rectangle with attributes length and width, each of which
defaults to 1.
Provide member functions that calculate the perimeter and the area of the
rectangle.
Also, provide set and get methods for the length and width attributes. The set
functions should verify that length and width are each floating point numbers
larger than 0.0 and less than 20.0.
The get functions should get the variables from the user.
Head of Department 7 Examiner
18- Define a jagged array that contains 3 rows. The number of elements for
each row is variable and can be defined by the user. Write a program to add
up (sum) all elements of the array. See the following example.

1 0 2 3
-1 1 Sum = 7
2 -2 1

Success,

Dr. Hakem Beitollahi

Head of Department 8 Examiner

You might also like