Vous êtes sur la page 1sur 8

Lahore University Of Management Sciences

School of Science and Engineering

Roll No : Total Pages:


Semester: Fall
Introduction to
Course Title: Academic Year:
Programming 2016–2017
Course Code: CS 200 Date: 23rd Oct, 2016
Naveed Arshad / 120 min
Instructor: Syed Irteza Time Allowed:
Exam: Midterm Total Marks: 40

The instructions below must be followed strictly. Failure to do so can result in serious grade loss.
DO NOT OPEN THIS EXAM UNTIL TOLD
TO DO SO. Keep your eyes on your own paper.

Read all questions very carefully before answering You may not
them.
talk to anyone once the exam begins.
Check the number of papers in the question sheet and leave the examination room and then
make sure the paper is complete. return.

Specific instructions:
Open book/notes, help sheet: Closed Book/Closed Notes/ No Help Sheet
Calculator usage: Not required
Write in pen/pencil: Anything legible, but neatly please
Any other instruction(s): Please Attempt all questions. Be concise and answer in
accordance to the allocated marks. Try to avoid overwriting/
cutting, etc.
Marks
1 2 3 4 5 6 7 Total

1|Page
Question 1) Please write the output of the following code? (3)

#include <iostream>
2
using namespace std;
void swap(int &a, int b) 5
{
int temp = a; 5
a = b;
b = temp;
}
int main()
{
int a = 4;
int b = 2;
int c = 5;
swap(a,b);
swap(b,c);
cout << a << endl;
cout << b << endl;
cout << c << endl;
return 0;
}

Question 2: What is wrong with the Strings cannot be used with switch statement
following piece of code fragment? (2)

string name;
name = “John”; // line 2
switch(name)
{
case “Smith”: cout << “smith”;
break;
case “John”: cout << “John”;
break
}

2|Page
Question 3: The following program finds the common elements in two different integer arrays (fibArray
and primeArray) and stores them in another array called commonArray. At the end of the program, it
prints out how many common elements there are. There are five bugs in the code. Identify them and
then fix them? (5)

#include <iostream>
using namespace std;
int main()
{
int fibArray[] = { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 };
int primeArray[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };
int commonArray[];
int i, j;

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


{
for ( j = 0; j < 10; ++j )
{
if (fibArray[i] = primeArray[j])
{
commonArray[j] = primeArray[j];
++n;
}
}
}
cout >> n;
return 0;
}

Line 5: commonArray has no size. Fix: int commonArray[10];

Line 6: n is not declared. Fix: add declaration int n;

Line 12: assignment operator used instead of equality operator. Fix: change = to ==.

Line 14: logic bug. Index to commonArray should be n, not j. Fix: change j to n.

Line 18: cout >> n is wrong. It should be cout << n

3|Page
Question 4: Describe what the following function is doing. Assume that the arguments to myfunction
have the same array size: (5)
The function copies all characters from s to t in reverse order.
void myfunction(char *s, char *t)
{
int i = 0, n = 0;
char p;
n = strlen(s);
while ( (p = *s++) != ’\0’ )
{
t[n-i-1] = p;
i++;
}
return;
}

Question 5: Find the output of the following code? (5)

#include <iostream> 10123


using namespace std;
int main()
{
int a = 1;
int b = 1;
while ( b < 3 )
{
switch ( a )
{
case 0:
b--;
case 1:
a--;
break;
case 3:
b++;
case 4:
a++;
break;
default:
b++;
}
cout << b << " ";
}
return 0;
}

4|Page
Question 6: Following is a recursive function.

int rec_func(int u, int k) {


if (k == 0)
return u;
return rec_func(u * k, k - 1);
}

int main()
{
int result = rec_func(5, 5);
return 0;
}

Part A: What will this function return with the following call in main? (1)

600

Part B: What is the maximum memory usage of this program in bytes if it is called with the following
actual parameters? (Assume the program does not have any other variables other than the one
mentioned in the code above) (2)

52 bytes or closest answer

Part C: If one converts the function rec_func into an iterative implementation? What is the maximum
memory usage the program? (2)

12 bytes or closest answer

5|Page
Question 7: Consider the following program?

1 #include <iostream>
2 using namespace std;
Y=3
3 const int X = 3;
4 void Proc1 (int &Y) X=7
5 {
6 Y = 6;
7 cout << "Inside Proc1, X: " << X << " Y: " << Y << "\n";
8 }
Z=3
9 int Proc2 (int &Y, const int Z)
10 {
11 int X = 7;
12 Proc1 (X);
Y=1
13 Proc1 (Y);
14 Y = 3;
15 cout << "Inside Proc2, X: " << X << " Y: " << Y << " Z: " << Z << "\n"; C=5
16 return (Y + Z);
17 }
18 int main()
19 { B=3
20 int A = 1;
21 int B = 3;
22 int C = 5;
23 C = Proc2 (A,B); A=1
24 cout << "Finally, A: " << A << " B: " << B << " C: " << C << "\n";
25 return(0);
26 }
X=3
Part A: Complete the depiction of the stack memory at line # 13? Mark the
function boundaries in the stack? (3)

Part B: Which variables are accessible and modifiable at line# 13? If aliases exist write their names also?
(2)

Accessible: Y/A, Z/B, X


Modifiable: Y/A, X

6|Page
Question 8) Write the output of the following code? (5)

#include <iostream>
using namespace std;

int main()
{
int a = 6;
int b = 5;
int c = 10;
int *aptr = &a;
int *bptr = &b;
int *cptr = &c;
cout << "Value of a: " << a << endl;
cout << "Value of b: " << *bptr << endl;
cout << "Value of c: " << c << endl;
*aptr = *aptr + *cptr;
c = c - *bptr;
cout << "Value of a: " << a << endl;
cout << "Value of b: " << b << endl;
cout << "Value of c: " << *cptr << endl;
return 0;
}

Value of a: 6

Value of b: 5

Value of c: 10

Value of a: 16

Value of b: 5

Value of c: 5

7|Page
Question 9: What does the following code do, in a brief English sentence?

int mystery(int k)
{
int i = 0;
if (k < 0)
return -1;
while (k > 0)
{
if (k % 3 == 0)
i++;
k--;
}
return i;
}

It returns floor(k/3) when k is positive and -1 when k is negative.

8|Page

Vous aimerez peut-être aussi