0% found this document useful (0 votes)
21 views8 pages

PF Sessional 2

Uploaded by

i242086
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

PF Sessional 2

Uploaded by

i242086
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

National University of Computer and Emerging Sciences

School of Computing Fall 2022 Islamabad Campus

CS-1002 Programming Serial No:


Sessional II
Fundamentals BS(DS/AI) Total Time: 1 Hour
Thursday, November 10, 2022
Total Marks: 60
Course Instructor
Mr. Adil Majeed, Ms. Ayesha Kamran, Dr. Hasan Mujtaba
Signature of Invigilator

Student Name Roll No Section Signature

DO NOT OPEN THE QUESTION BOOK OR START UNTIL INSTRUCTED.


Instructions:
1. Attempt on the question paper. Attempt all of them. Read the question carefully, understand the question, and
then attempt it.
2. No additional sheet will be provided for rough work. Use the back of the last page for rough work.
3. If you need more space, write on the back side of the paper ( only for questions number II, III and IV ) and mark
the question and part number etc.
4. After being asked to commence the exam, please verify that you have (8) different printed pages, including this
title page. There is a total of (4) questions.
5. Use of a calculator is strictly prohibited.
6. Use permanent ink pens only. Any part done using a soft pencil will not be marked and cannot be claimed for
rechecking.
7. Use proper indentation while writing code and ensure that your code is legible. You will lose your marks If
your code is not clear.
8. Please read the question thoroughly and use your time properly; an uneven time distribution can lead to incom-
plete answers.

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

Question I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (36 Marks)


Only the code of main function is written without return 0 statement. So please don’t identify those errors.
(a) (2 Marks) What will the following program display on screen. Explain the error or bug if there is any.
1 int a, b, c;
2 a=6,b=4,c=2;
3 int max = (a>b>c)*(a+b+c);
4 cout<<max;

(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

Question III . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . (8 Marks)


(a) (8 Marks) Write a function in c++ which takes a number n as input (parameter) and return sum of all digits
except unit digit (rightmost digit), is equal to the unit digit(rightmost). If the function returns the sum of
digits, then print YES in main otherwise print NO.
For Example, if the user enters 1124 it will print YES because 1+1+2 is equal to 4
While if the user enters 2348 it will print No because 2+3+4 is not equal to 8.

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

You might also like