CS-116 - Programming Fundamentals - End Semester Exam
CS-116 - Programming Fundamentals - End Semester Exam
IMPORTANT INSTRUCTIONS:
You must follow general guideline for students before online examination and during
online examination which had already shared by email and WhatsApp.
This paper has a total of _06__ pages including this title page
Q.1. (10)
Analyze and put down the values of variables a, b and c after the execution of each line. Show
step by step working for each statement.
Note: You have to write your own 2-digit roll number against RollNumber wherever mentioned.
int main() a b c
{ int a=0;int b=1; int c=2; int d=3;
a = b + c * d << RollNumber && 8;
a = a && RollNumber != 3;
a = a == b || a == c && RollNumber < 5;
c = x != 0;
a = -1 + RollNumber-- - 5 + ++c;
a = b == c++;
a = b = c = 0;
c = 7 - -5 + 23 / RollNumber != 45 && b > 7;
a= a-b/c;
c = 7 - -5 + 23 / 34 != RollNumber && b > 7 + ++a / 1 ||sizeof(int);
return 0;}
Q.2. (10)
Dry run the following lines of C++ code. Each statement is performed independently and not
connected with previous.
Note: You have to assign your own roll number to variable b given below before solving.
V. e = a + (b - c) / d * c + a; // 20 + (150/5)
cout << "Value of a + (b - c) / d * c + a is :" << e << endl ;
VI. cout << "Value of b++ and ++b is: “ << b++ << ++b;
VII. cout << "Value of ++b + a++ is: “ << ++b + a++;
VIII. cout << "Value of d++ % b++ is: “ << d++ % b++;
IX. cout << "Value of b++ << ++c << ++b << c++ is: “ << c++ << ++c << ++b <<
b++;
Q.3. (10)
A.
Number guessing games are quite interesting. Design a program for a guessing game,
which allows N number of tries to a user for guessing a randomly generated number
where N equals the integer value calculated by formula given below. At each wrong try,
the program informs the user whether the input number is either larger or smaller than
guess_num. The program prints “Congrats! You won.” if the user guessed the number
correctly. If user can’t guess correct number in N tries, then program should print
“Unfortunately You lost”.
N = ( (RollNumber+20) % 5 ) + 4
Note: Students are required to provide their own Roll Number in above formula to
calculate the value of N.
B.
Draw the flowchart for problem statement of part A. Flowchart must be drawn on plain
paper.
Q.4. (10)
A.
Suppose there are two variable length character arrays containing first name and last name of
student. Both array values are to be entered by user at run time. Write a program to merge
and sort both of the arrays in a third character array in ascending order based on ASCII
values of each character and then display the final output and total length of third array on
screen (don’t count spaces in total length). You can use bubble sort algorithm for sorting
purpose.
Note: (ASCII values of Capital letters A-Z are 65-90 and of small letters a-z are 97-122).
Use only Capital letters or only small letters at one time as shown in sample runs below.
Sample Run 1
Enter First Name: ALI
Enter First Name: NADEEM
After merging and sorting: A A D E E I L M N
Total Length of final Array: 9
Sample Run 2
Enter First Name: amir
Enter First Name: khan
After merging and sorting: a a h i k m n r
Total Length of final Array: 8
B.
Write down the step by step algorithm for problem statement of part A.
Note: Solve this part on plain paper.
Q.5. Specify the logical and/or syntax errors in following chunks of code and re-write the
correct version of code. Justify your answer with proper reasoning for each statement.
Note: There might be more than one errors and no error at all. (10)
A.
#include<iostream.h>
void main()
{
int i= RollNumber, j=5; // Assign your own roll number to variable i
int modResult=0;
int divResult=0;
modResult = i%j;
cout<<modResult<<" ";
divResult = i/modResult;
cout<<divResult;
}
B.
int main()
{
int i = 0;
for(i = 0; i < 3; i++);
{
cout<<"loop ";
continue;
}
getchar();
return 0;
}
C.
int main()
{
int x=3, y=4, z=5;
for(x=0;x<z;x++)
{
for(y=0;y<=x;y++)
{
if(a=='Z')
break;
a++;
}
cout<<endl;
}
}
D.
int x = 2;
switch(x)
{
case 2:
cout << "two" << endl;
case 3:
cout << "three" << endl;
}
E.
#include<iostream>
int main()
{
int i=1;
while()
{
cout<<"i++”<<endl;
if(i>RollNumber) // Put 1st digit of your Roll No in if condition
break;
}
return 0;
}