National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
Serial No:
CS 1002: Programming Sessional II Exam
Fundamentals Total Time: 01 Hours
Friday November 18, 2022 Total Marks: 55
Course Instructor
________________
Engr. Shehzad Ahmad Signature of Invigilator
____________________________ _______ __________ _____________________
Student Name Roll No Section Signature
DO NOT OPEN THE QUESTION BOOK OR START UNTIL INSTRUCTED .
Instructions:
1. Verify at the start of the exam that you have a total of three (3) questions printed
on ten (10) pages including this title page.
2. Attempt all questions on the question-book and in the given order.
3. The exam is closed books, closed notes. Please see that the area in your threshold
is free of any material classified as ‘useful in the paper’ or else there may a charge
of cheating.
4. Read the questions carefully for clarity of context and understanding of meaning
and make assumptions wherever required, for neither the invigilator will address
your queries, nor the teacher/examiner will come to the examination hall for any
assistance.
5. Fit in all your answers in the provided space. You may use extra space on the last
page if required. If you do so, clearly mark question/part number on that page to
avoid confusion.
6. Use only your own stationery and calculator. If you do not have your own
calculator, use manual calculations.
7. Use only permanent ink-pens. Only the questions attempted with permanent ink-
pens will be considered. Any part of paper done in lead pencil cannot be claimed
for rechecking.
Q-1 Q-3 Q-3 Total
Total Marks 15 20 20 55
Marks Obtained
Assessment of CLO CLO 1 CLO 3 CLO 4
Vetted By: ____________________________Vetter Signature: ____________________
Q1.
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
CLO-01: Recognize connection between byte values, data types, C++ variables, their
addresses and contents and number systems.
a. For the following variables initialization, find the final value of each variable and then
draw a single memory map that shows various addresses and byte contents (both in
hex) for each variable. The starting address value is given. [2x5=10]
Address Byte Value Variable
bool b = 0; (hex) (hex) name
bool a = (3>2 || 3+2)
char c = ‘b’; 345678
short s = 0xFEED;
int x = −230−221−21;
b. What amount of memory is allocated for each of the following variable declarations?
Sessional II Exam Fall 2022 Page 2 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
[5]
i. char str[]= {‘p’,‘a’,‘p’,‘e’,’r’};
ii. char str[]= “paper”;
iii. float num = 8.0;
iv. int input[5];
v. char op = 65;
Q2.
CLO-3: Code a design problem using C++ constructs, such as input/output statements,
arithmetic, relational and logical operators, control structures and repetition statements
with nesting.
a. For each code snippet given below find the output: [3+4+3]
int main()
{
int num = 0;
num++;
if (num > 0)
{
num++;
if (num > 0)
num++ ;
else
num--;
}
if (num > 0)
num++;
else if (num > 1)
num--;
else
num--;
cout<<num;
}
int main()
{
char alphabet;
for(int i = 1; i <= 5; ++i)
{ alphabet = 'A';
for(int j = 1; j <= i; ++j)
{
cout << alphabet++ << " ";
}
Sessional II Exam Fall 2022 Page 3 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
cout << endl;
}
return 0;
}
int main()
{
int a[5] = {19, 59, 3, 41, 15};
int b[5];
int i = 0, j;
for(j = 4; i < 5; i++, j--)
{
b[ j ] = a[ i ];
}
i = 0;
for( ; i< 5 ; )
{
cout<<b[ i ]<<"\t";
i++;
}
return 0;
}
b. Write a program to evaluate cosine series, where x is angle in radians: [10]
2 4 6 8
x x x x
cos ( x )=1− + − + −. . . .. . .upto N terms
2! 4! 6! 8!
The general formula for above series is written below:
N−1
(−1 )n x 2 n
cos ( x )= ∑
n=0 (2 n) !
Sample Run:
Computing Series of cos(x):
-------------------------------------
Enter value of N: 5
Enter value of x: 0.785 (i.e. pi/4)
Ans. from given series : 0.707
Sessional II Exam Fall 2022 Page 4 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
Q3.
CLO-4: Use one-dimensional static arrays of C++ built-in data types in programming
problems.
Sessional II Exam Fall 2022 Page 5 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
a. Write a program which counts number of even and odd elements in an array. Ask
the user to enter 10 integer values. [10]
Sessional II Exam Fall 2022 Page 6 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
b. Write a program which takes a decimal value without decimal point as input and
stores into a char array called num. The program displays the int variable sum which
is the sum of the digits of the decimal value. [10]
Sample Run 1:
Input decimal value without decimal point: 424
sum = 4+2+4=10
Sample Run 2:
Input decimal value without decimal point: 3245
sum = 3+2+4+5=14
#include <iostream>
using namespace std;
int main () {
char num[100] = {0};
int sum =0;
cout<<”Input decimal value without decimal point”<<endl;
cin>>num;
Sessional II Exam Fall 2022 Page 7 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
Page left intentionally blank for rough work
Sessional II Exam Fall 2022 Page 8 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
Sessional II Exam Fall 2022 Page 9 of 10
National University of Computer and Emerging Sciences
School of Engineering Islamabad Campus
ASCII TABLE
Sessional II Exam Fall 2022 Page 10 of 10