Sessional-II-Solution Exam Paper Fall 2022
Sessional-II-Solution Exam Paper Fall 2022
Serial No:
CS-1002: Programming
Sessional Exam-II
Fundamentals (CS) Total Time: 1 Hour
Total Marks: 60
Thursday, 17th November, 2022 ________________
Course Instructors Signature of Invigilator
1.1: Consider the following C++ code and answer the questions below.
#include <iostream>
using namespace std;
int main() {
const int N=3;
int A[N]={3,2,1};
int B[N]={0};
for(int i = 0; i < N; ++i)
{
int length = 1;
while (A[i] != 1) {
if (A[i] % 2)
A[i] = A[i] * 3 + 1;
else
A[i] /= 2;
++length;
}
B[i]= length;
}
return 0;
}
a. What are the contents of array B when 1st iteration of for loop terminates.
b. What are the contents of array B when 2nd iteration of for loop terminates.
c. What are the contents of array B when 3rd iteration of for loop terminates.
Page 2 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
1.2: Consider the following C++ code and answer the questions below.
int get(int N)
{
static int i = 0;
return N-(i++) - 1;
}
int main()
{
int SIZE = 10;
int arr[] = {5,6,7,8,9,10,11,12,13,14};
int j;
for(int i=0; i < SIZE; i++)
{
j = get(SIZE);
if (j==i)
continue;
arr[i] += arr[j];
arr[j] = arr[i] - arr[j];
arr[i] -= arr[j];
}
return 0;
}
a. What are the contents of array after 2nd iteration of for loop?
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]
14 13 7 8 9 10 11 12 6 5
arr[0] arr[1] arr[2] arr[3] arr[4] arr[5] arr[6] arr[7] arr[8] arr[9]
5 6 7 8 9 10 11 12 13 14
Page 3 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
Question 2 [5+2+3+5+5+5+5+5= 35 Marks]
For the following questions, write the output of the program in the given box. In case of no output write NO
Output with the reason (No marks without mentioning the reason).
int list[5]={2,4,8,10,-1};
int nextList[5]={3,-1,0,1,-1};
int start = 2;
int Free = 4;
void magic(int val , int position){
int start = ::start;
for(int i = 0 ; i< position - 1 ; i++)
start=nextList[start];
list[Free]=val; nextList[Free]=nextList[start];
nextList[start]=Free++;
}
void magic(){
int start = ::start;
while(start != -1){
cout<<list[start]<<"->";
start=nextList[start];
}
cout<<"*"<<endl;
}
int main()
{
magic();
magic(5,2);
magic();
return 0;
}
Output:
8->2->10->4->*
8->2->5->10->4->*
Page 4 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
C++ Program Output
16.5
float calc(int y, int x) {
return (y + x + 7.0 / 2);
}
int main()
{
float i = 9.5;
int j = 4.5;
cout << calc(i, j) << endl;
}
}
cout << endl;
Page 5 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
6
int x, y = 4; 5
for (x = 2; x < y ; x+=2)
y = y + 1 % x;
cout << y << endl;
x = y;
do {
cout << --x << endl;
x *= 4;
} while (x <= 10);
int r=5,x=0;
while(x<r){
int y=1;
while(y<r-x){
cout<<" ";
y++;
}
int z=0,n=1;
while(z<=x){
n= z==0 || x==0 ? 1 : n*(x-z+1)/z;
char ch = n==1 ? 'X' : n%3==0 ? 'Y' : 'V'
;
cout<<ch<<' ';
z++;
}
cout<<'\n';
x++;
}
Page 6 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
Description:
Log base 2, also known as binary logarithm which is the inverse function of the power of two functions. The
general logarithm states that for every real number n, can be expressed in exponential form as
n=ax
Following function finds log base 2 of 32 bit integer
int log2(int x)
{
int res = 0;
while (x >>= 1)
res++;
return res;
}
a. x >>= 2
b. x >>= 1
c. x <<= 2
d. x <<= 1
e. x ~= 2
Description:
Following functions checks if given integer is power of 2
bool isPowerof2(int x)
{
return (x && !(x & x-1));
}
a. (x && !(x && x-1))
b. (x || !(x || x-1))
Page 7 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
Description:
Given an array arr[] of size N-1 with integers in the range of [1, N], the task is to find the missing number
from the first N integers.
Note: There are no duplicates in the list.
Examples:
Input: arr[ ] = {1, 2, 4, 6, 3, 7, 8}, N = 8
Output: 5
Explanation: The missing number between 1 to 8 is 5
#include <iostream>
using namespace std;
int main()
{
int arr[ ] = { 1, 10, 3, 7, 5, 6, 9, 2, 8 };
const int N = sizeof(arr) / sizeof(arr[0]);
int i;
int temp[N + 1];
N &= N;
for(int i = 0; i <= N; i++){
temp[i] = 0;
}
int ans;
for (i = 0; i <= N ; i++) {
if (temp[i] == 0)
ans = i + 1;
}
cout <<ans<<endl;
}
a. arr[temp[i] - 1] = 1;
b. arr[temp[i] + 1] = 1;
c. temp[arr[i] - 1] = 1;
d. temp[arr[i] + 1] = 1;
Page 8 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
ROUGH WORK
Note: anything written on this page will not be marked.
Page 9 of 10
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
ROUGH WORK
Note: anything written on this page will not be marked.
Page 10 of 10