PF Sessional 2
PF Sessional 2
I II III IV Total
Total Marks 36 8 8 8 60
Marks Obtained
CS-1002 Programming Fundamentals BS(DS/AI) Fall 2022 Sessional II
(b) (3 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 char alphabet= 'A';
2 for(int i = ('F'-'A'+1); i >= 1; --i)
3 {
4 for(int j = 1; j <= i; ++j)
5 {
6 cout << alphabet << " ";
7 }
8 ++alphabet;
9 cout << endl;
10 }
(c) (3 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 int i, j, m, answer;
2 m = 0;
3 j = 4;
4 while (m < 5) {
5 for (i = 0; i < j; i++){
6 answer = i * m;
7 cout << answer;
8 }
9 m = m + 1;
10 cout << endl;}
(d) (1 Mark) What is the output of the following program segment? Identify errors (if any).
1 int x = 5, y = 10;
2 int z = ++x * y--;
3 cout<<(z+y);
(e) (2 Marks) What is the output of the following program segment? Identify errors (if any).
1 int x=10;
2 {
3 cout<<x<<"\t";
4 int x=20;
5 cout<<(x++)<<"\t";
6 }
7 cout<<(--x);
(f) (1 Mark) What is the output of the following program segment? Identify errors (if any).
1 int z = 5, j = 7, k = 6, n = 3;
2 cout << (z + j % k + k * n - 15) << "\t" ;
3 cout << (z % n + 5) << endl;
Page 2 of 8
CS-1002 Programming Fundamentals BS(DS/AI) Fall 2022 Sessional II
(g) (2 Marks) What is the output of the following program segment? Identify errors (if any).
1 int i = 12, counter = 5;
2 while (i - 1)
3 {
4 ++counter;
5 i--;
6 }
7 cout<<counter;
(h) (1 Mark) What is the output of the following program segment? Identify errors (if any).
1 int a=5;
2 int b=a++++;
3 cout<<b;
(i) (2 Marks) What is the output of the following program segment? Identify errors (if any). Assume uninitial-
ized variable has value zero in beginning.
1 int x=10;
2 {
3 int x=x;
4 cout<<x<<"\t";
5 }
6 cout<<(--x);
(j) (2 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 int suite = 5 ;
2 switch ( suite ) ;
3 {
4 case 0+5 ;
5 cout<< "\nClub" ;
6 case 1+5 ;
7 cout<< "\nDiamond" ;
8 }
Page 3 of 8
CS-1002 Programming Fundamentals BS(DS/AI) Fall 2022 Sessional II
(k) (1 Mark) What is the output of the following program segment? Identify errors (if any). Assume uninitial-
ized variable has value zero in beginning.
1 int i=0, n = 0;
2 if ((i < 1) && (++i < n))
3 {
4 cout << "Condition True!";
5 }
6 else
7 cout<<"Not True";
(l) (2 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 int x = 8, y = 0, z ;
2 while ( x >= 0 && y <=5 )
3 {
4 if ( x == y )
5 break ;
6 else
7 cout<<x<<y ;
8 x-- ;
9 y+=2 ;
10 }
(m) (2 Marks) What is the output of the following program segment? Identify errors (if any).
1 int z, x=5, y=-10, a=4, b=2;
2 z = x++ - --y * b / a;
3 cout<<z;
(n) (3 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 int x = 1, y =2, n=50 ;
2 while (y <=n )
3 {
4 if ( n%y == 0 )
5 {
6 n=n/y;
7 x=x+1;
8 }
9 else
10 y=y+1;
11 cout<<x<<y<<" ";
12 }
(o) (2 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 int a = 0, b=26;
2 float f=2.6;
3 b+= (a = 50)*f/2*10-b\%5;
4 cout << a << "#" << b;
Page 4 of 8
CS-1002 Programming Fundamentals BS(DS/AI) Fall 2022 Sessional II
(p) (2 Marks) What is the output of the following program segment? Identify errors (if any).
1 float Mystery(int y, int x){
2 return (y + x + 7.0 / 2); }
3 int main(){
4 float i = 9.5;
5 int j = 4;
6 cout << Mystery(i, j) << endl; }
(q) (2 Marks) What is the output of the following program segment? Identify errors (if any).
1 int fun(int x){
2 return x % 3 + 1; }
3 int main() {
4 int b = 5;
5 int y = 2 + fun(3 * b + 1);
6 int z = fun(fun(y));
7 cout << y << "-" << z; }
(r) (3 Marks) Convert the following code (of switch) in if-else decision structures.
1 cin >> sport;
2 switch (sport) {
3 case 'c':
4 cout << "You like Cricket";
5 case 'f':
6 cout << "You like Football";
7 break;
8 case 't':
9 case 'H':
10 cout << "You like Tennis";
11 cout << "You like Hockey";
12 case 'B':
13 cout << "You like BasketBall";
14 break; }
Page 5 of 8
CS-1002 Programming Fundamentals BS(DS/AI) Fall 2022 Sessional II
Question II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (8 Marks)
(a) (8 Marks) Write a function in c++ which takes a number as input and prints the pattern of alphabets as given
below. You are required to validate the input that the number should be in-between 1 and 26.
For example, for n=5, print pattern as below
A B C D E D C B A
B C D E D C B
C D E D C
D E D
E
for n=4, print pattern as below
A B C D C B A
B C D C B
C D C
D
Page 6 of 8
CS-1002 Programming Fundamentals BS(DS/AI) Fall 2022 Sessional II
Page 7 of 8
CS-1002 Programming Fundamentals BS(DS/AI) Fall 2022 Sessional II
Question IV . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (8 Marks)
(a) (8 Marks) The value of a function F is defined by the following infinite series:
1 x2 x4 x6 x8
F = + + + + + ···
3 9 27 81 243
Write a C++ code that takes n (number of terms) and the value of x as input from the user and calculates the
function’s value. In other words, calculate the sum of the series for the first N terms. For example, if n=5 and
x=2, then it should display 0.744856.
Page 8 of 8