0% found this document useful (0 votes)
17 views14 pages

S23 CS101 Endsem Solutions Rubrics

Uploaded by

kebopix215
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)
17 views14 pages

S23 CS101 Endsem Solutions Rubrics

Uploaded by

kebopix215
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/ 14

CS 101 (Spring 2023) — 24 April 2024

End-semester Examination
(Instructor: Shivaram Kalyanakrishnan)

Roll Number
Solutions

Division and Group NO partial marks unless specified

Name

Q. No. Marks Graded By Verified By Student’s Cribs

1 (3)

2 (2)

3 (3)

4 (3)

5 (2)

6 (4)

7 (2)

8 (3)

9 (4)

10 (4)

TOTAL
Please read the following instruc- Translation
tions carefully before you start.
• शुरू करने से पहले कृपया िनम्निलिखत िनदर्े शाें को ध्यान
• Write your roll number, name, and से पढ़ें ।
group number in the space provided.
A paper without a roll number and • िदए गए स्थान पर अपना रोल नं बर, नाम और ग्रुप नं बर
name will NOT be graded. िलखें । िबना रोल नं बर और नाम के पे पर को grade नहीं
िदया जाएगा।
• Write your answers neatly with a
blue/black pen on this question paper • इस प्रश्नपत्र पर ही प्रत्ये क प्रश्न के िलए िदए गए स्थान
itself in the space provided for each पर अपने उत्तर नीले /काले पे न से साफ-सुथरा िलखें । अं त
question. At the end, you must submit में आपको यह पे पर िनरीक्षक के पास जमा करना होगा।
this paper to the invigilator. • रफ पे ज उपलब्ध नहीं कराए जाएं गे. margin में िदए
• Rough pages will NOT be provided. खाली जगह का उपयोग करें .
Use the empty space in the margins. • कृपया ध्यान दें िक आपके उत्तराें में कोई भी प्रोग्रािंमग
• Please note that your answers should अवधारणा शािमल नहीं होनी चािहए िजसे अब तक कक्षा
NOT include any programming con- में शािमल नहीं िकया गया है । यिद ऐसे उत्तर पाए जाते
cept that hasn’t been covered in the हैं , तो उन्हें grade नहीं िकया जाएगा।
class so far. If such answers are found, • िकसी भी प्रश्न पर कोई स्पष्टीकरण नहीं िदया जाएगा.
they shall NOT be graded. जब सं देह हो, तो उपयुक्त धारणाएँ बनाएं , उन्हें स्पष्ट रूप
• No clarifications will be provided से बताएं और समस्या को हल करने के िलए आगे बढ़ें ।
on any questions. When in doubt, यिद आपका उत्तर आपके द्वारा की गई िकसी धारणा पर
make suitable assumptions, state िनभर्र करता है , तो धारणा को आपके paper में स्पष्ट
them clearly, and proceed to solve the रूप से बताया जाना चािहए
problem. If your answer depends on • कुछ प्रश्नाें में आपको code snippet िदए गए हैं । मान
any assumption you have made, the लें िक code snippet main program के अं दर
assumption must be explicitly stated िलखा है , सही header files include की गयी हैं ,
in your paper. आिद, और इसिलए, code compile होता है ।
• In some questions, you are provided • शुभकामनाएं !
code snippets. Assume that the code
snippet is enclosed suitably within
the main, correct header files are in-
cluded, etc., and therefore, the code
compiles.

• All the best!


Question 1

/////////////////////////////////// Consider the program given in the column


#include <simplecpp> on the left. In the box given below, write
down its output. [3 marks]
#define LENGTH 40 बाइर्ं ओर के column में िदए गए program पर िव-
#define WIDTH 30 चार करें । नीचे िदए गए box में इसका output िलखें । [3
#define M 100 marks]

void f1a(){
double area = LENGTH * WIDTH; (1 mark each)
cout << "f1a: " << area << "\n";
} f1a: 1200
float g1b(float a[M], float b[M], f1b: 8.7
int n){
if(n == 0){ f1c: 0
return 0;
}
float x = (a[n - 1] * b[n - 1]);
x += g1b(a, b, n - 1);
return x;
}

void f1b(){
float a[] = {0, 2.1, -1, 3}; Rough work ↓
float b[] = {3.8, 1, 0, 2.2};
cout << "f1b: " << g1b(a, b, 4)
<< "\n";
}

void f1c(int n){


int product = 1;
for(int i = n; i >= 0; i--){
product *= i;
}
cout << "f1c: " << product
<< "\n";
}

main_program{
f1a();//1 mark
f1b();//1 mark
f1c(6);//1 mark
}
///////////////////////////////////

3
Question 2

/////////////////////////////////// Consider the program given in the column


#include <simplecpp> on the left. In the box given below, write
#define LETTERS 26 down its output. [2 marks]
बाइर्ं ओर के column में िदए गए program पर िव-
void g2a(char word[100]){ चार करें । नीचे िदए गए box में इसका output िलखें । [2
marks]
int letters[LETTERS];
for(int i = 0; i < LETTERS; i++){
letters[i] = 0; a:4 c:1 d:1 h:1 n:2 r:1 y:1
}
int w = 0; 2 marks: if the letters and
while(word[w] != '\0'){ their frequencies are exactly
if(word[w] >= 'a' && the same and are in the same
word[w] <= 'z'){ order
letters[word[w] - 'a'] += 1;
} 1 mark: (a) if there are four
else if(word[w] >= 'A' && or more pairs whose letters and
word[w] <= 'Z'){ frequencies are correct, but
letters[word[w] - 'A'] += 1; not all. (b) if there are any
} additional letters or missing
w++; letters.
}
for(int i = 0; i < LETTERS; i++){ 0 marks: if less than four such
if(letters[i] > 0){ pairs
char c = i + 'a';
cout << c << ":"
<< letters[i] << " ";
}
}
cout << "\n";
}
Rough work ↓
void f2a(){
char word[] = "Chandrayaan-3";
g2a(word);
}

main_program{
f2a();//2 marks
}
///////////////////////////////////

4
Question 3

3a. When you compile a program, the 3c. Suppose files p1.cpp and p2.cpp both
compiler can generate both “errors” and contain valid c++ programs. Also, the file
“warnings”. What is the main difference p1.cpp is larger in size (in terms of num-
between an error and a warning? Write ber of bytes) than p2.cpp. Now, suppose
your answer in the box below. [1 mark] upon compilation, p1.cpp gives executable
3a. जब आप िकसी program को compile करते हैं , तो file a.out.1, whereas upon compilation,
compiler “errors” और “warnings” दोनाें gen- p2.cpp gives executable file a.out.2.
erate कर सकता है । Errors और warnings के बीच Is it necessary that a.out.1 is larger in
मुख्य अं तर क्या है ? अपना उत्तर नीचे िदये गये box में िलखें । size (in terms of number of bytes) than
[1 mark] a.out.2? Provide your answer (“Yes” or
Errors (0.5 marks): it denotes “No”), along with 1–2 lines of supporting
that the program has syntax justification, in the box below. [1 mark]
errors and the compilation 3c. मान लीिजए की files p1.cpp और p2.cpp दोनाें
is unsuccessful. Thus, वै ध c++ programs contain करते है । साथ ही, file
the executable file is not p1.cpp size में बड़ा है (number of bytes के मुता-
generated िबक) p2.cpp से । अब, मान लीिजए compilation पर,
warning (0.5 marks): this p1.cpp, a.out.1 executable file दे ता है , जबिक
means that the compiler has p2.cpp compilation पर, a.out.2 executable
identified some problems but file दे ता है ।
may not be major issues. The क्या यह आवश्यक है िक a.out.1 size में (bytes
executable file gets generated. की सं ख्या के सं दभर् में ) a.out.2 से बड़ा हो? अपना उत्तर
For example, declaring a (“Yes” या “No”) नीचे िदए गए box में , justification
variable but not using it will 1-2 पं िक्तयाें के साथ प्रदान करें । [1 mark]
lead to a warning.
3b. Match each entry (numbers) in the left
column (Column 1) with the most related (1 mark) (0.5 marks each)
entry (letters) in the right column (Column
2). E.g. (1)B, (2)A, and so on. [1 mark] No
बाएं column (column 1) में प्रत्ये क entry
The size of the executable
(numbers) को दाएं column (column 2) में सबसे
depends on the code written and
सं बंिधत entry (letters) के साथ िमलाएं । जै से (1)B,
not the size of the file. For
(2)A, इत्यािद। [1 mark]
example, comments in the source
code (any number, any size)
Column 1 Column 2 will not increase the size of
(1) Compiler (A) #define the executable.
(2) File (B) cout
Rough work ↓
(3) Output (C) std
(4) Macro (D) fstream
(5) Name space (E) g++

Fill in the letter below.


0.2 marks/blank
(1) E (2) D (3) B (4) A (5) C

5
Question 4

/////////////////////////////////////// Given in the column on the left are three


#include <simplecpp> different implementations of a function
that is meant to print out all the prime
void f4a(int n){ factors of a given positive integer n, but
for(int i = 2; i <= n; i++){ excluding n. For example, if the input is
if(n % i == 0){ n = 84, the output must be the following
bool prime = true; (recall that 1 is not prime).
for(int j = 2; 237
j <= i && prime; j++){ If n is prime, there must be no out-
if(i % j == 0) prime = false; put. Go through the three implementa-
} tions, and for each, assess if (1) the func-
if(prime) cout << i << " "; tion compiles without error, and if it does
} (2) the function produces the expected out-
} put for all valid inputs. Then fill out the
} 6 blank cells below with “Yes” or “No”. [3
marks]
void f4b(int n){ बाइर्ं ओर के column में एक function के तीन अलग-
for(int i = 2; i * i <= n; i++){ अलग implementations िदए गए हैं , िजसका उद्देश्य
if(n % i == 0){ िकसी िदए गए positive integer n के सभी prime
bool prime = false; factors को print करना है , ले िकन यिद यह स्वयं prime
for(int j = 2; है तो n को छोड़कर। उदाहरण के िलए, यिद input n = 84
j * j <= i && !prime; j++){ है , तो output िनम्निलिखत होना चािहए। याद रखें िक 1
if(i % j != 0) prime = true; prime नहीं है
} 237
if(prime) cout << i << " "; यिद n prime है , तो कोई output नहीं होना चािहए।
} तीन implementations को दे खें, और प्रत्ये क के िलए,
} आकलन करें िक क्या (1) function, error के िबना
} compile होता है , और यिद (2) function सभी वै ध in-
put के िलए अपे िक्षत output उत्पन्न करता है । िफर नीचे 6
void f4c(int n){ blank cells “Yes” या “No” से भरें । [3 marks]
for(int i = 2; i * i <= n; i++){
if(n % i == 0){
bool prime = true; f4a f4b f4c
for(int j = 2; Compiles with
j * j <= i && prime; j++){ no errors Yes Yes Yes
if(i % j == 0) prime = false;
} Produces expected
if(prime) cout << i << " "; output No No No
}
} 0.5 marks each
}
///////////////////////////////////////

6
Blank space for rough work ↓ Blank space for rough work ↓

7
Question 5

/////////////////////////////////////// Consider the program given in the column


#include <simplecpp> on the left. In the box given below, write
struct S5a{ down its output. [2 marks]
char a; बाइर्ं ओर के column में िदए गए program पर िव-
int b; चार करें । नीचे िदए गए box में इसका output िलखें । [2
S5a(){ marks]
a = 'a'; b = 0;
}
S5a(char w, int x){
a = w; b = x;
} (0.5 marks each)
void setChar(char w){ f5a: c -1
a = w;
} (1 mark)
void setInt(int x){ f5b: 70.5
b = x;
}
};
Rough work ↓
void f5a(){
S5a s('c', 3);
s.setInt(-1);
cout << "f5a: " << s.a
<< " " << s.b << "\n";
}

double g5b(float a, int b){

return (a + b / 2 + (a * b) / 2);
}

void f5b(){
cout << "f5b: "
<< g5b(15.0, 7) << "\n";
}

main_program{
f5a();//1 mark
f5b();//1 mark
}
///////////////////////////////////////

8
Question 6

//////////////////////////////////// Consider the program given in the column


#include <simplecpp> on the left. In the box given below, write
down its output. [4 marks]
double g6a(double x, int n){ बाइर्ं ओर के column में िदए गए program पर िव-
if(n == 1){ चार करें । नीचे िदए गए box में इसका output िलखें । [4
return x; marks]
}
return (x * g6a(x, n - 1));
}
2 marks each
double h6a(double x, int n){
if(n == 1){ f6a: 65536
return x;
} f6b: 31104
return g6a(x, h6a(x, n - 1));
}

void f6a(){
cout << "f6a: "
<< h6a(2.0, 4) << "\n";
}
Rough work ↓
int g6b(int m, int n){
if(m == 0 && n == 0){
return 1;
}
if(m == 0){
return (3 * g6b(m, n - 1));
}
return (2 * g6b(m - 1,n));
}

void f6b(){
cout << "f6b: "
<< g6b(7, 5) << "\n";
}

main_program{
f6a();//2 marks
f6b();//2 marks
}
////////////////////////////////////

9
Question 7

7a. Consider the sequence T:

4, 1, 16, 9, 36, 25, 64, 49, . . . . T[i] = (((i+1)%2) + (i/2)*2 + 1)


* (((i+1)%2) + (i/2)*2 + 1)
We number the terms starting from index
0; hence T[0] = 4, T[1] = 1, T[2] = 16, and so If the student has written a
on. The pattern is: for each non-negative different expression, check and
even integer i, T[i] = (i + 2)2 , and for each see if it works. However, only
non-negative odd integer i, T[i] = i2 . i 1 2 + - * / % or parentheses
Express T[i] as a c++ expression that should be used by the student
only uses symbols from the list below. in the answer
i 1 2 + - * / %

In particular, you cannot use condi-


tional constructs such as if and the ?:
(“ternary”) operator. Nor can you use ad-
ditional variables for intermediate stor-
Rough work ↓
age. The challenge is to obtain a formula
that works both for even and for odd i.
Write your answer in the box at the top of
the right column. [1 mark]

7a. Sequence को consider करें T:

4, 1, 16, 9, 36, 25, 64, 49, . . . .

हम terms को index 0 से शुरू करते हैं । इस तरह T[0]


= 4, T[1] = 1, T[2] = 16, .... Pattern है : प्रत्ये क
non-negative even integer i के िलए, T[i] = (i+
2)2 , और प्रत्ये क non-negative odd integer i के
िलए, T[i] = i2 .
T[i] को एक c++ expression के रूप में व्यक्त करें
जो केवल नीचे दी गई symbols का उपयोग करती है ।

i 1 2 + - * / %

िवशे ष रूप से , आप conditional constructs जै से


िक if और ?: (“ternary”) operator का उपयोग
नहीं कर सकते । न ही आप additional variables या
तो intermediate storage का उपयोग कर सकते हैं ।
चुनौती एक ऐसा formula प्राप्त करना है जो even और
odd i दोनाें के िलए काम करे । अपना उत्तर दाएँ column
के शीषर् पर िस्थत box में िलखें । [1 mark]

10
7b. Let A be an n × n matrix represented
in a program as a 2-dimensional array. k=i * n + j
Hence, for i ∈ {0, 1, . . . , n − 1} and j ∈ OR
{0, 1, . . . , n − 1}, A[i][j] is the entry of A in A mathematical expression like
its i-th row and j-th column. k = in + j
Now suppose that the contents of A are
read into a single-dimensional array B of
size n2 , using the following code snippet. If the student has written a
different expression, check and
int k = 0; see if it works. However, only
for(int i = 0; i < n; i++){ i, j, and n should be used by
for(int j = 0; j < n; j++){ the student in the answer
B[k] = A[i][j];
k++;
}
}

For which value of k is the entry A[i][j] Rough work ↓


assigned to B[k]? In other words, express
k in terms of i and j such that B[k] is equal
to A[i][j]. Write your answer in the box at
the top of the right column. [1 mark]
7b. मान लीिजए A एक n × n matrix है िजसे pro-
gram में 2-dimensional array के रूप में दशार्या
गया है । इसिलए, i ∈ {0, 1, . . . , n − 1} और j ∈
{0, 1, . . . , n − 1} के िलए, A[i][ j] इसकी i-वीं row
और j-वें column में A की entry है ।
अब मान लीिजए िक A की contents को िनम्निलिखत
code snippet का उपयोग करके n2 size के single-
dimensional array B में read िकया जाता है ।

int k = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
B[k] = A[i][j];
k++;
}
}

k के िकस value के िलए entry A[i][j] को B[k]


को साैं पा गया है ? दूसरे शब्दाें में , k को i और j के रूप में
इस प्रकार व्यक्त करें िक B[k] और A[i][j] के बराबर हो।
अपना उत्तर दाएँ column के शीषर् पर िस्थत box में िलखें ।
[1 mark]

11
Question 8

//////////////////////////////////// Consider the program given in the column


#include <simplecpp> on the left. In the box given below, write
down its output. [3 marks]
void f8a(){ बाइर्ं ओर के column में िदए गए program पर िव-
cout << "f8a: "; चार करें । नीचे िदए गए box में इसका output िलखें । [3
int i = 0; marks]
while(i < 8){
cout << "x";
int j = 0;
while(j * j < i){
cout << j; (2 marks)
j++; f8a: xx0x01x01x01x012x012x012
} 2 marks: if the entire sequence
i++; is exactly the same i.e. the
} blue part and the red part
cout << "\n";
} 1 mark: (a) if the sequence
in blue is exactly the same.
double g8b(double a1, double a2, Ignore the sequence in the red
double b1, double b2, or (b) if the sequence in red
double &c1, double &c2){ is exactly the same. Ignore the
c1 = (a1 + b1) / 2; sequence in blue.
c2 = (a2 + b2) / 2;
return ((a1 + a2 + b1 + b2) / 4); 0 marks: if both the sequences
} i.e. red and blue, both have
some mismatches
void f8b(){
double a1 = 0; (1 mark)
double a2 = 1; f8b: 0.625 4.25 -3
double b1 = 8.5;
double b2 = -7;
double c1 = 0;
double c2 = 0;
Rough work ↓
double z = g8b(a1, a2,
b1, b2, c1, c2);
cout << "f8b: " << z << " "
<< c1 << " " << c2 << "\n";
}

main_program{
f8a();//2 marks
f8b();//1 mark
}
////////////////////////////////////

12
Question 9
////////////////////////////////////////
Consider the program given in the column
#include <simplecpp>
on the left. In the box given below, write
down its output. [4 marks]
double g = 9.81;
बाइर्ं ओर के column में िदए गए program पर िव-
चार करें । नीचे िदए गए box में इसका output िलखें । [4
class C9a{
marks]
double x;
public:
C9a(){
x = 0; (1 mark each)
cout << "C9a: g = " << g << "\n"; (No partial marks for f9d.
g = -g; The entire sequence should be
} correct)
double getX(){ return x;}
}; C9a: g = 19.62
class C9b{ C9b: y.x = 0
C9a y;
public: f9c: g = -19.62
C9b(){
cout << "C9b: y.x = " f9d: 0 1 1 2 1 2 2 3
<< y.getX() << "\n";
}
};

void f9c(){ Rough work ↓


g = 2.0 * g;
C9b z;
cout << "f9c: g = " << g << "\n";
}

void f9d(){
cout << "f9d: ";
for(int n = 0; n <= 7; n++){
int right = n % 2;
int mid = ((n - right) / 2) % 2;
int left =
((n - (2 * mid) - right) / 4) % 2;
cout << (right + mid + left) << " ";
}
cout << "\n";
}

main_program{
f9c();//2 marks
f9d();//2 marks
}
////////////////////////////////////////

13
Question 10 10b. Where is the program’s output
stored? What is its relation with the input?
Carefully go through the program shown
If necessary explain using an example. [2
below and answer the following ques-
marks]
tions.
10b. Program का output कहाँ store िकया जाता
है ? इसका input से क्या सं बंध है ? यिद आवश्यक हो तो
नीचे िदखाए गए program को ध्यानपूवर्क पढ़ें और िन-
उदाहरण दे कर समझाएँ । [2 marks]
म्निलिखत प्रश्नाें के उत्तर दें ।
////////////////////////////////////////
#include <simplecpp> (a) The program's output is
#include <fstream> stored in two files, "out1.txt"
and "out2.txt". (b) Based on
main_program{ the random number selected,
i.e. if it is less than 0.5,
ifstream ifile("in.txt"); the program stores one of
ofstream ofile1("out1.txt"); the input number in either
ofstream ofile2("out2.txt"); out1.txt, else in out2.txt.
int a[1000]; That is, each number in in.txt
int ctr = 0; is written with probability
while(ifile >> a[ctr]){ half into out1.txt, and with
ctr++; probability half into out2.txt.
} Example: if input sequence is
srand(time(NULL)); [1, 2, 3, 4, 5] random numbers
for(int i = 0; i < ctr; i++){ generated are: [0.2, 0.6, 0.1,
if(randuv(0, 1) < 0.5){ 0.5, 0.3] then 1, 3, and 5 are
ofile1 << a[i] << "\n"; stored in out1.txt while 2 and
} 4 in out2.txt
else{
ofile2 << a[i] << "\n"; 10c. If the statement srand(time(NULL)) is
} replaced by srand(0), what would be the
} effect on the execution of the program? [1
} mark]
//////////////////////////////////////// 10c. यिद statement srand(time(NULL)) को
10a. What is the source of the program’s srand(0) से बदल िदया जाए, तो program के exe-
input? What is the expected input format? cution पर क्या प्रभाव पड़े गा? [1 mark]
[1 mark]
10a. Program के input का source क्या है ? अपे -
िक्षत input format क्या है ? [1 mark] It would seed random number
with a fixed value 0 instead
of current time. i.e. every
(a) The source is a file time the program is executed,
"in.txt". (b) The expected the same random numbers will
input format is a sequence be generated and we can predict
of integers separated by the behavior of the program.
whitespace or newline So, the same output files will
characters. get generated each time.

Rough work ↓

14

You might also like