You are on page 1of 10

SCHOOL OF INFORMATION AND COMMUNICATIONS TECHNOLOGY

ALBUKHARY INTERNATIONAL UNIVERSITY



MIDTERM EXAMINATION-SCHEMA
COURSE CODE : CSC1013
COURSE NAME : INTRODUCTION TO OBJECT ORIENTED PROGRAMMING
DATE/DAY : 12
TH
NOV 2012 / MONDAY
TIMES (HOURS) : 0800 PM 1000 PM (2 HOURS)
VENUE : ICT 03-03, ICT 03-04, ICT 03-05, ICT 03-06
INSTRUCTIONS

A. There are THIRTY-TWO(32) questions in THREE(3) parts
Part 1(10 Questions), Part2(20 Questions) and Part 3(2
Questions)
B. ANSWER ALL QUESTIONS.
C. Write your answer in the space provided.

MARKS

Student Name :
Student Number : - -
(in words)

Lecture group :

Lecturer Name :

CONFIDENTIAL

2

PART A: True/False question (10 Questions, 10 Marks)
1.
C# source code is a High Level Language that must be compiled before it can be
executed
True
2.
A variable represents a named location in computer hard disk to store a piece of
data.
False
3.
C# is considered as a strong types Programming language. This mean we have to
implicitly tell the compiler what types of data that we want to store in a variable.
True
4.
A minus(-) operator can be a unary or binary operator True
5.
int and long are types use for variable that hold the whole numbers, whereas float
and bool hold a fraction numbers.
False
6. If animal1 = dog and animal2 = cat, then the value of
animal1.CompareTo(animal2) is a positive number.
TRUE
7. The statement creates an array named price and stores four values within the array:
double [ ] price = new double [4] {{35.70,78.20},{69.40,21.55}};
FALSE
8. The default statement is used to end a case expression in a switch block FALSE
9. The type of identifier or variable used in the foreach expression must match the
array type
TRUE
10. Substring() methods used to locates the first occurrence of a character or substring
in a string and returns its index, or -1 if it is not found
FALSE

PART B: Short Questions (20 Questions, 60 Marks)
1. What will be the output from the following program?
(2 Marks)
class ABC
{
public void Main()
{
string value1 = number;
int value2 = 10;
System.Console.Write("{1} is his {0}", value1, value2);
System.Console.WriteLine( and also my number.);
}
}
10 is his number and also my number.



2. What is the difference between a value data type and a reference data type? Give one example of
each.
(3 marks)
A value data type stores actual values at the location identified by the data types name.
(1mark)
3

Example: structure, enumeration (0.5 mark)
A reference data type stores a location that points to where the information is stored. (1 mark)
Example:class, array (0.5 mark)






3. Explain what is the use of the following statement?
(3 marks)
Value = int.Parse(Console.Readline());
It will read a string from the console(1 mark), convert it to integer and assign it to the variable
namely Value (2 marks)

4. The following code snippet has a problem. Identify the error and fix it.
Assume that MyArray is a one dimensional array of decimal values.
foreach (decimal Element in MyArray)
{
Console.WriteLine(Element Value is : {0}, Element);
Element = Element * Element;
Console.WriteLine(Element squared is : {0}, Element);
}
(4 marks)
Error: Cannot assign to 'Element' because it is a 'foreach iteration variable' (1 mark)

for (int i = 0; i < MyArray.Length; i++ ) (1 mark)
{
Console.WriteLine("Element Value is : {0}", MyArray[i]); (1 mark)
MyArray[i] *= MyArray[i]; (1 mark)
Console.WriteLine("Element squared is : {0}", MyArray[i]); (1 mark)
}





5. What will be the result of this program?
(3 marks)
class XYZ
{
public void Main()
{
int x=0,y=0,z=0;
y = ++x;
z = x++;
System.Console.Write("x={0} , y={1} , z={2}", x, y,z);
}
}
x=2 , y=1 , z=1

4

6. What is meant by operator precedence? Give an example of an expression that uses more than two
different operator that illustrate the operator precedence.
(5 3marks)

The operator precedence determines the order in which they are processed. (1 mark)
Example: y = a + b * c; b*c will be done first before adding to a since * have higher precedence.
(2 marks)

7. When used with a while statement, which jump statement causes execution to halt inside the loop
and immediately transfers control to the conditional expression?
(1 mark)
continue

8. Write a block of code using if-else-if statement in the following program to decide the grade as
shown in the following table:-
Marks Grade
>= 90 A
70 89 B
50 69 C
< 50 D
(5 marks)
class grade
{
public void Main()
{
int marks;
char grade;
System.Console.Write( Enter your marks: );
marks = int.Parse(System.Console.ReadLine());

//write your code here

If(marks > 90) grade = A; (2 marks)
else if (marks > 69) grade = B; (1 mark)
else if (marks > 49) grade = C; (1 mark)
else grade = D; (1 mark)


System.Console.WriteLine(Marks +marks+ grade +grade);
}
}



9. What is the output for this program code?
struct MyStruct
{
int x ;
int y ;
public void SetXY(int i, int j)
{
5

x = i;
y = j;
}
public void ShowMultiply()
{
int multi = x * y;
Console.WriteLine("The multiplication is {0}", multi);
}
}
class MyClient
{
public static void Main()
{
MyStruct ms = new MyStruct();
ms.SetXY(100, 200);
ms.ShowMultiply();
Console.ReadLine();
}
}
(2 marks)

The multiplication is 20000


10. What will be the output of this code?
(4 marks)
class myCase
{
public void Main()
{
for( int x=1; x<6; x++ )
{ switch( x )
{ case 2: Console.WriteLine("x is {0} -- In Case 2", x);
break;
case 5: Console.WriteLine("x is {0} -- In Case 5", x);
break;
default: Console.WriteLine("x is {0} -- In Default case", x);
break;
}
}
}
}
x is 1 -- In Default case
x is 2 -- In Case 2
x is 3 -- In Default case
x is 4 -- In Default case
x is 5 -- In Case 5
(2 mark all in order, 1 mark default case, 1 mark case number)

11. Convert program in number 6 so that it use if-else statement rather than the switch statements.
(5 marks)
class myIf
{
public static void Main()
{
6

for (int x = 1; x < 6; x++)
{
if(x == 2) Console.WriteLine("x is {0} -- In Case 2", x);
else if(x == 5) Console.WriteLine("x is {0} -- In Case 5", x);
else
Console.WriteLine("x is {0} -- In Default case", x);
}
Console.ReadKey();
}
}

12. What is the output for this program code?
for (int number = 0; number < 5; number++)
{
if (number > 3)
continue;

Console.WriteLine("Value of number is {0}", number);
}
(2 marks)
Value of number is 0
Value of number is 1
Value of number is 2
Value of number is 3


13. Write a code to declare a single dimensional array type string and initialize it to Jane, Hanis,
Lokman and Saiful.
(3 marks)
string[ ] Name = new string[4] {Jane, Hanis, Lokman , Saiful};



14. Write a block of code using while loop to add all even numbers (i.e 2,4,6,) from 0 to 1000.
(5 marks)
int x = 0;
int sumEven = 0;
while (x <= 1000)
{
sumEven = sumEven + x;
x = x + 2;
}

15. Define rank and length with related to array implementation.
(2 marks)
Rank: The number of dimensions an array has. (1 mark)
Length: The total number of elements contained in an array, in all dimensions. (1 mark)

16. What will be produced from this block of code?
(2 marks)
int x = 0;
do
{
7

Console.WriteLine("x: {0}", x);
x--;
} while (x > 0);
Console.WriteLine("Out of loop");
x: 0 (1 mark)
Out of loop (1 mark)

17. What is a jagged array and write a code to declare a jagged array of type int with size of [3].
(5 marks)
Jagged array is an array whose elements are arrays. The elements of a jagged array can be of
different dimensions and sizes. A jagged array is sometimes called an "array of arrays." ( 2marks)
int[][] jagged = new int[3][]; (0.5 mark)
jagged[0] = new int[4]; (0.5 mark)
jagged[1] = new int[3]; (0.5 mark)
jagged[2] = new int[5]; (0.5 mark)




Question 18 and 19 is using the following program code for an array declaration.

int [ ] MyArray = new int [10];

18. Write a foreach loop to display the contents of MyArray.
(2 marks)
foreach (int num in MyArray)
Console.Write(num );



19. Write a for loop to increment each element in MyArray by 35.
(2 marks)
for (int i = 0; i < MyArray.Length; i++)
{
MyArray[i] += 35; // MyArray[i] = MyArray[i] + 35;

}


20. What will be the output from the following code?
class Program
{
static void Main()
{
for (int i = 0; i < 5; i++)
{
for (int j = i; j < 5; j++);
Console.Write(i);
Console.WriteLine();
}
}
}
(2 marks)
8

0
1
2
3
4

Part C: Program Code Writing (2 Questions, 30 Marks)

1. Write a program to tabulate values of the function
(15 marks)



Your program should ask for the range of x. Make sure your program check for the ending
values of x must be greater than the starting values of x.
Using the range, find and display all values of y.
The program should prompt the user whether to continue with different range of x or exit.
The output may looks as follows:-

Enter the starting values of x: 2
Enter the ending values of x: 4

When x = 2, y= -13
When x = 3, y= 6.5
When x = 4, y= 3.9

Do you want to continue? Y
Enter the starting values of x: 5
Enter the ending values of x: 4

The range is incorrect.
Do you want to continue? N

HINT: You may use Math.Pow() method to calculate the expression.


9


Answer:
class Program
{
static void Main()
{
int startX, endingX; // 2 marks
double Y;
string cont = "Y";
while (cont == "Y") // 2 marks for while loop
{
Console.Write("Enter the starting values of x: "); // 0.25 mark
startX = int.Parse(Console.ReadLine()); // 0.25 mark
Console.Write("Enter the ending values of x: "); // 0.25 mark
endingX = int.Parse(Console.ReadLine()); // 0.25 mark
if (startX > endingX) // 4 marks for if statement block
{
Console.WriteLine("Starting values must be less than ending values");
continue;
}
for (int i = startX; i <= endingX; i++) // // 4 marks for for statement block
{
Y = ((2 * Math.Pow(i, 2)) + (3 * i) - 1) / (Math.Pow(i, 2) - 5); // 2 marks
Console.WriteLine("When x = {0}, y={1} ", i, Y);
}
Console.Write("Do you want to continue (Y/N)? :");
cont = Console.ReadLine();
}
}
}

2. Write a program to enter 15 test marks into one dimensional array of type int. Give the name of
array is Score[ ].
Find the average mark
Find the lowest mark
Find the highest mark

HINT: Set the initialization of lowest and highest to the first element of Score[ ].

The output may look as below:
Enter Score 1: 90
Enter Score 2: 80
Enter Score 3: 70
Enter Score 4: 60
Enter Score 5: 85
Enter Score 6: 75
Enter Score 7: 88
Enter Score 8: 92
Enter Score 9: 53
Enter Score 10: 67
Enter Score 11: 81
Enter Score 12: 59
Enter Score 13: 64
10

Enter Score 14: 69
Enter Score 15: 82

Average: 74.33
Lowest score: 53 Highest score: 92
(15 marks)
using System;
namespace MyScore
{
public class MyScoreMark
{
public static void Main()
{
int Total = 0; // 1 mark
double Average; // 1 mark
int[] score = new int[15]; // 1 mark

//Values are entered.
for (int i = 0; i < score.Length; i++) // 3 marks
{
Console.Write("Enter Score {0}: ", i + 1);

score[i] = int.Parse(Console.ReadLine());
}

//Values are summed and check for lowest and high marks.
int high = score[0], low = score[0];
for (int i = 0; i < score.Length; i++) // 3 marks
{

Total += score[i]; // Total = Total + score[i];
if (low > score[i]) low = score[i]; // 2 marks
if (high < score[i]) high = score[i]; // 2 marks
}

Average = Total / score.Length;
Console.WriteLine();
Console.WriteLine("Average: {0:f} ", Average); // 1 mark
Console.WriteLine("Lowest score: {0} \tHighest score: {1} ", low, high);
// 1 mark

Console.ReadLine();
}
}
}

You might also like