[go: up one dir, main page]

0% found this document useful (0 votes)
15 views38 pages

CS101 TSC

The document contains a series of programming questions and their solutions, primarily focused on C++ programming concepts such as operator precedence, variable scope, loops, and function calls. Each question is followed by a solution that provides the expected output or behavior of the code snippets. The document serves as a study guide for understanding basic programming principles and debugging techniques.
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)
15 views38 pages

CS101 TSC

The document contains a series of programming questions and their solutions, primarily focused on C++ programming concepts such as operator precedence, variable scope, loops, and function calls. Each question is followed by a solution that provides the expected output or behavior of the code snippets. The document serves as a study guide for understanding basic programming principles and debugging techniques.
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/ 38

CS101 TSC

Autumn 2024-25
Samar Perwez
with special thanks to Shravya Suresh
Question 1
What is the Output?

#include <iostream>
using namespace std;
int main()
{
int a = 5; int b = 7;
int c = 2; int d = 9;
int e = a * b % c + d;
cout<<"e="<<e<<"\n";
int i = a++ + ++b - c--;
cout<<"i="<<i<<"\n";
int f = d * i / 2;
float f2 = d * i / 2;
cout<<"f="<<f<<" f2="<<f2<<"\n";
}
Solution 1
What is the Output?
e=10
i=11 #include <iostream>
using namespace std;
f=49 f2=49
int main()
{
int a = 5; int b = 7;
int c = 2; int d = 9;
Operator Precedence int e = a * b % c + d;
cout<<"e="<<e<<"\n";
int i = a++ + ++b - c--;
Pre/Post Inc/Dec cout<<"i="<<i<<"\n";
int f = d * i / 2;
float f2 = d * i / 2;
cout<<"f="<<f<<" f2="<<f2<<"\n";
Datatypes }
Question 2

Consider the following badly indented snippet. What is the output if a=5, b=7, c=9? What is
the output if a=4, b=4, c=4? Can the program ever output “AAA CCC ZZZ”? (Yes or No)

if(a >= 5)
if(b < 6)
cout<<"AAA ";
else
cout<<"BBB ";
else if(c == 9)
cout<<"CCC ";
cout<<"ZZZ\n";
Solution 2
BBB ZZZ Consider the following badly indented snippet. What
is the output if a=5, b=7, c=9? What is the output if
ZZZ a=4, b=4, c=4? Can the program ever output “AAA
CCC ZZZ”? (Yes or No)
No
if(a >= 5)
if(b < 6)
Corresponding if and cout<<"AAA ";
else
else blocks
cout<<"BBB ";
else if(c == 9)
cout<<"CCC ";
cout<<"ZZZ\n";
Question 3
Find the output:

int a = 5;
if (a > 1){
int a = 6;
cout << a <<endl;
a++;
}
cout << a <<endl;
Solution 3
Find the output: Output:

int a = 5; 6
if (a > 1){ 5
int a = 6;
cout << a <<endl;
a++;
}
cout << a <<endl; Scope of Variables
Question 4

Consider the following code snippet. What is the output if N=52? What does this program
compute? [Write answer as a mathematical function of N]

int N;
cout<<"Enter number: ";
cin>>N;
int x = 0;
int y = 1;
while( y < N) {
y = y*2;
x++;
}
cout<<x;
Solution 4
6 Consider the following code snippet. What is the
output if N=52? What does this program compute?
⌈log2N⌉ [Write answer as a mathematical function of N]

int N;
cout<<"Enter number: ";
While Loops cin>>N;
int x = 0;
int y = 1;
while( y < N) {
y = y*2;
x++;
}
cout<<x;
Question 5
What is the output of this program?

#include<simplecpp>

int func(int &a){ int func2(int b){


a = 1; return 2*b;
cout<<func2(a); }
return 0;
} int func2(int b, int c){
return b*c;
main_program{ }
int a = 10;
func(a);
cout << func(a) - a << endl;
}
Solution 5
Output:
What is the output of this program?

#include<simplecpp>
2
int func2(int b){
return 2*b; 2
int func(int &a){ }
a = 1;
-1
cout<<func2(a); int func2(int b,
return 0; int c){
} Pass by reference
return b*c;
}
main_program{
int a = 10;
func(a);
Overloading
Cout << func(a)
- a << endl;
}
Question 6
Consider the following program
int N;
cout<<"Enter number: ";
which takes an input a positive
cin>>N; integer N, and prints a N X N
int i = 0; matrix whose diagonal elements
int j = 0; (on both diagonals) are 1, and the
while(i < N) { remaining elements are 0. Fill in
j = 0;
while(j < N) {
the blanks [A, B, C1, C2]
if(_A_) cout<<"1";//fill A
else cout<<"0"; e.g. N = 5
_B_; //fill B 10001
} 01010
_C1_; //fill C1 00100
_C2_; //fill C2 01010
} 10001
Solution 6
int N;
cout<<"Enter number: "; Pattern Printing
cin>>N;
int i = 0;
int j = 0;
while(i < N) {
j = 0;
while(j < N) {
if(i==j || i+j==N-1)//fill A
cout<<"1";
else
cout<<"0";
j++; //fill B
}
cout<<"\n"; //fill C1
i++; //fill C2
}
Question 7
Outputs again!

1 # include < iostream >


2 using namespace std;
3 int main ()
4 {
5
6 int FACT = 1;
7 for (unsigned int i = 6; i >= 0; --i)
8 {
9 FACT *= i;
10 }
11 cout << FACT << endl ;
12 return 0;
13 }
Solution 7
Outputs again!

1 # include < iostream >


2 using namespace std;
Infinite loop! (Why?)
3 int main () How do we fix this?
4 {
5
6 int FACT = 1;
7 for (unsigned int i = 6; i >= 0; --i)
8 {
9 FACT *= i;
10 }
11 cout << FACT << endl ;
12 return 0;
13 }
Solution 7
Outputs again!
Change the condition on i
1 # include < iostream > OR
2 using namespace std;
3 int main () Just remove ‘unsigned’
4 {
5
6 int FACT = 1;
7 for (unsigned int i = 6; i > 0 or i > 1; --i)
8 {
9 FACT *= i;
10 }
11 cout << FACT << endl ; Loop conditions
12 return 0;
13 }
Question 8
Consider the following program which takes an input a positive integer N, and
prints the number of 7s in the number. Fill in the blanks [A, B, C]

#include <iostream>
using namespace std;
int main() {
int num;
cout<<"Enter number: ";
cin>>num;
int count = 0; int digit;
while (_A_) { //fill A
digit = _B_; //fill B
num = _C_; //fill C
if(digit == 7) count++;
}
cout<<count;
}
Solution 8
#include <iostream>
using namespace std; Digit based problems
int main() {
int num;
cout<<"Enter number: ";
cin>>num;
int count = 0; int digit;
while ( num > 0 ) { //fill A
digit = num % 10; //fill B
num = num / 10; //fill C
if(digit == 7) count++;
}
cout<<count;
}
Question 9
Complete the following code so the program outputs the square root of y (y>0) The
precision is given as 0.001.

main_program {
double y; cin >> y;
double x = 1, eps = 0.001;

while(________) {
x = (x + y/x) / 2;
}

cout << x << endl;


}
Solution 9
Complete the following code so the program outputs the square root of y (y>0) The
precision is given as 0.001.

main_program {
double y; cin >> y;
double x = 1, eps = 0.001;

while( abs(x*x - y) > eps ) {


x = (x + y/x) / 2;
}

cout << x << endl; Understanding the


} intent of the code
Question 10
What is the Output?

#include <iostream>
using namespace std;
int main() int func1(int &b, int &a, int c=6)
{ {
int a = 5, b = 7; int ret = 2*a+b+c;
int c; int temp = b;
c = func1(a, b); b = a;
cout<<"a = "<<a<<endl; a = temp;
cout<<"b = "<<b<<endl; return ret;
cout<<"c = "<<c<<endl; }

}
What is the Output?
Solution 10 #include <iostream>
using namespace std;
a = 7 int func1(int &b,int &a);

b = 5 int main()
{
c = 25 int a = 5, b = 7;
int c;
c = func1(a, b);
cout<<"a = "<<a<<endl;
Function Calls cout<<"b = "<<b<<endl;
cout<<"c = "<<c<<endl;
}
int func1(int &b,int &a, int c=6)
{
Call by Reference int ret = 2*a + b + c;
int temp = b;
b = a;
a = temp;
Local Variables return ret;
}
Question 11
Aaaaaaand another output!

main_program {
for (int i = 0; i < 8; i++);
{
if (i%2 == 0)
cout << i + 1 << " ";
else
cout << i << " ";
}
cout << "EOP";
}
Question 11
Aaaaaaand another output!

main_program {
for (int i = 0; i < 8; i++);
{
if (i%2 == 0)
cout << i + 1 << " "; Okay, a few things…
else
cout << i << " ";
}
cout << "EOP";
}
Question 11
Aaaaaaand another output!

main_program { Interesting? This actually works!


for (int i = 0; i < 8; i++); Think of it like an empty loop.
{
if (i%2 == 0) It iterates over itself with no other operation.
cout << i + 1 << " ";
else
cout << i << " ";
}
cout << "EOP";
}
Question 11
Aaaaaaand another output!

main_program {
for (int i = 0; i < 8; i++);
{ But error here!
if (i%2 == 0) i is not declared outside
cout << i + 1 << " "; the scope of the ‘for’ loop
else
cout << i << " ";
}
cout << "EOP";
}
Solution 11 (with modified code)
Aaaaaaand another output!

main_program {
int i;
for (int i = 0; i < 8; i++); What’s the output?
{
if (i%2 == 0)
cout << i + 1 << " ";
else
cout << i << " ";
}
cout << "EOP";
}
Solution 11 (with modified code)
Aaaaaaand another output!

main_program {
int i;
for (i = 0; i < 8; i++); Ans. 9 EOP
{ // i = 8 at this point
if (i%2 == 0) // 8%2 = 0, so true
cout << i + 1 << " "; // 8+1 = 9
else
cout << i << " ";
}
cout << "EOP"; Read code carefully; not
}
everything is an error :)
Question 12
Given the following program:
main_program {
for(int outval=0; outval<_A_; outval++){
for(int inval=0; inval<=outval; inval++){
cout<<(inval+_B_);
}
cout<<endl;
}
}

Complete the code such that the output is as shown below:


0
12
234
3456
Solution 12
Given the following program:
main_program {
for(int outval=0; outval < 4 ; outval++){
for(int inval=0; inval<=outval; inval++){
cout<<(inval + outval );
}
cout<<endl;
}
}

Complete the code such that the output is as shown below:


0
12
Understanding the purpose
234
3456 of code through the output
Question 13
Decimal equivalent of 101.1012

Binary equivalent of 9.7510


Question 14

What is the Output?

#include <iostream>
using namespace std; int fun_bitwise(int x, int y)
int main() {
{ int z = x | (1<<y);
int a = 9, b = 4; cout<<"z = "<<z<<endl;
int c; int y = x ^ y;
c = fun_bitwise(a, b); cout<<"y = "<<y<<endl;
cout<<"c = "<<c<<endl; return (z & y);
}
}
What is the Output?
Solution 14 #include <iostream>
using namespace std;
z = 25 int fun_bitwise(int x, int y)
{
y = 13 int z = x | (1<<y);
cout<<"z = "<<z<<endl;
c = 9 int y = x ^ y;
cout<<"y = "<<y<<endl;
return (z & y);
}
int main()
{
int a = 9, b = 4;
int c;
Bitwise Operators c = fun_bitwise(a, b);
cout<<"c = "<<c<<endl;

}
Question 15
Given the following program, what is output?
#include <iostream>
void countCalls() {
static int count = 0;
count++;
cout << "Function called " << count << " times" <<endl;
}

int main() {
countCalls();
countCalls();
countCalls();
}
Solution 15
Given the following program:

#include <iostream>
void countCalls() { Function called 1 times
static int count = 0;
count++; Function called 2 times
cout << "Function called " <<
count << " times" <<endl; Function called 3 times
}

int main() {
countCalls();
countCalls();
countCalls();
Static variables
}
Question 16
Given the following program, what is output?
#include <iostream>
struct MyStruct {
char a;
int b;
char c;
int d;
};
int main() {
MyStruct s1 = {'A', 1024, 'F', 300};
MyStruct s2;
s2.a = s1.a + 3;
s2.b = s1.d;
s2.d = s1.b
s2.c = s2.a - 2;
cout<< s2.a << “ ” << s2.b << “ ” << s2.c << “ ” << s2.d;
}
Solution 16
Given the following program:
#include <iostream>
struct MyStruct {
char a; D 300 1024 B
int b;
char c;
int d;
};
int main() {
MyStruct s1 = {'A', 1024, 'F',
300};
MyStruct s2;
s2.a = s1.a + 3;
s2.b = s1.d; Structures
s2.d = s1.b
s2.c = s2.a - 2;
cout<< s2.a << “ ” << s2.b << “
” << s2.c << “ ” << s2.d;
}
All the best!

You might also like