0% found this document useful (0 votes)
60 views10 pages

Sessional-II-Solution Exam Paper Fall 2022

The missing number can be found by using the formula: Expected Sum - Actual Sum Where, Expected Sum of first n natural numbers = n(n+1)/2 Actual Sum = Sum of all elements in arr[] int missingNumber(int arr[], int n) { int expected_sum = n*(n+1)/2; int actual_sum = 0; for(int i=0; i<n-1; i++) actual_sum += arr[i]; return expected_sum - actual_sum; } int main() { int arr[] = {1, 2, 4, 6, 3, 7, 8}; int n = 8

Uploaded by

Awais Khattak
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)
60 views10 pages

Sessional-II-Solution Exam Paper Fall 2022

The missing number can be found by using the formula: Expected Sum - Actual Sum Where, Expected Sum of first n natural numbers = n(n+1)/2 Actual Sum = Sum of all elements in arr[] int missingNumber(int arr[], int n) { int expected_sum = n*(n+1)/2; int actual_sum = 0; for(int i=0; i<n-1; i++) actual_sum += arr[i]; return expected_sum - actual_sum; } int main() { int arr[] = {1, 2, 4, 6, 3, 7, 8}; int n = 8

Uploaded by

Awais Khattak
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/ 10

National University of Computer and Emerging Sciences

FAST School of Computing Fall-2022 Islamabad Campus

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

Dr. Aleem, Dr. Akhtar, Ms. Ifrah,


Mr. Sheheryar

____________________ ____________ _____________ __________________


Student Name Roll No. Course Section Student Signature

DO NOT OPEN THE QUESTION BOOK OR START UNTIL INSTRUCTED.


Instructions:
1. Attempt on 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 last page for rough work.
3. After asked to commence the exam, please verify that you have ten (10) different printed
pages including this title page. There are a total of 3 questions.
4. Calculator is strictly prohibited.
5. Use permanent ink pens only. Any part done using soft pencil will not be marked and cannot
be claimed for rechecking.

Q-1 Q-2 Q-3 Total


Marks
Obtained
Total
10 35 15 60
Marks
National University of Computer and Emerging Sciences
FAST School of Computing Fall-2022 Islamabad Campus
Question 1 [5+5=10 Marks]

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[0] B[1] B[2]


8 0 0

b. What are the contents of array B when 2nd iteration of for loop terminates.

B[0] B[1] B[2]


8 2 0

c. What are the contents of array B when 3rd iteration of for loop terminates.

B[0] B[1] B[2]


8 2 1

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

b. What are the contents of array after for loop terminates?

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->*

Each line 2.5 marks


Correct output at correct place

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;
}

float x = 10; No output. Infinite loop


while (x < 100) {
int x = 20;
x *= 5;
x -= 10;
}
cout << x << endl;

char text[] = "Hello WorlD"; #ELLO&#ORL#


int i = 0;
while (text[i] != '\0')
{
if (text[i] =='W'||text[i]=='H'|| text[i] == 'D')
text[i] = '#';
else if (text[i]>='a'&&text[i]<='z')
text[i] -= 32;
else if (i % 2 == 0)
text[i] = text[i] - 1;
else
text[i] = '&';
i++;
}
cout << text << endl;

int MAX = 70; B


for (char ch = 65; ch <= MAX; ++ch) { C
int i = 'A';
D D
while (true)
{ E E
if (i++ % 2 == 0) F F F
continue;
if (i > ch)
break;
cout << ch << " ";

}
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 i, j, sum = 10;


for (i = 0; i<5; i++) 31
if (i % 2)
for (j = 0; j <= 3; sum += j++); Note: correct output 5 marks
else
for (j = 3; j>0; sum += --j);
cout << sum;

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

Question 3 [5+5+5=15 Marks]


Complete the following C++ code by selecting the correct C++ statement or answer. Ambiguous selection
will be treated as false.

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

f. None of the above

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))

c. (x & !(x & x-1))

d. (x | !(x && x-1))

e. (x && !(x & x-1))

f. None of the above

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;
}

for(i = 0; i < N; i++){


temp[arr[i] - 1] = 1;
}

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;

e. None of the above

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

You might also like