0% found this document useful (0 votes)
45 views

Exercise 7 Intro To Programming

This document contains the questions and answers from an exercise on C++ programming concepts. It includes 12 multiple choice and coding questions covering topics like switch statements, if/else statements, for loops, while loops, and the conditional operator. The questions test understanding of control flow, input/output, and manipulating variable values based on conditions.

Uploaded by

The Godfather
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Exercise 7 Intro To Programming

This document contains the questions and answers from an exercise on C++ programming concepts. It includes 12 multiple choice and coding questions covering topics like switch statements, if/else statements, for loops, while loops, and the conditional operator. The questions test understanding of control flow, input/output, and manipulating variable values based on conditions.

Uploaded by

The Godfather
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Name: Muzammil

Section: B Electrical Engineering

FA19-BEE-078

Exercise #07

Question 01: Consider the following code fragment.

int x;
std::cin >> x;
switch (x + 3) {
case 5:
std::cout << x << '\n';
break;
case 10:
std::cout << x - 3 << '\n';
break;
case 20:
std::cout << x + 3 << '\n';
break;
}
1. It would print “2” on the screen.
2. It wouldn’t print anything.
3. It would print “4” on screen.
4. It would print “20” on screen.
5. It wouldn’t print anything.

Question 02: What will following code print?

char ch;
std::cin >> ch;
switch (ch) {
case 'a':
std::cout << "*\n";
break;
case 'A':
std::cout << "**\n";
break;
case 'B':
case 'b':
std::cout << "***\n";
case 'C':
case 'c':
std::cout << "****\n";
break;
default:
std::cout << "*****\n";
}
1. It would print 1 asterisk.
2. It would print 2 asterisks.
3. It would print 3 asterisks on 1 row and 4 on the second row.
4. It would print 3 asterisks on 1 row and 4 on the second row.
5. It would print 4 asterisks.
6. It would print 4 asterisks.
7. It would print 5 asterisks.

Question 03: What is printed by following code fragment.

int x = 0;
do {
std::cout << x << " ";
x++;
} while (x < 10);
std::cout << '\n';

It prints:
0123456789

Question 04: What’s printed by following code:

int x = 20;
do {
std::cout << x << " ";
x++;
} while (x < 10);
std::cout << '\n';

It prints “20” on the screen.

Question 05: What following code prints?

for (int x = 0; x < 10; x++)


std::cout << "*";
std::cout << '\n';

It prints 10 asterisks on the screen.


Question 06: Re-write the program given in question so that “switch” is used instead of if-else
statement.

#include<iostream>
using namespace std;
int main(){
int value;
char ch;
cout << "Enter any character: ";
cin >> ch;
switch(ch){
case 'A':
value = 10;
cout << value << '\n';
break;
case'P':
value = 20;
cout << value << '\n';
break;
case 'T':
value = 30;
cout << value << '\n';
break;
case 'V':
value = 40;
cout << value << '\n';
break;
default:
value = 50;
cout << value << '\n';
break;
}
}

Question 07: Re-write the program given in the question to use multi-way if-else statement instead of
switch?

#include<iostream>
using namespace std;
int main(){
int value;
char ch;
cin >> ch;
if (ch=='A') value = 10;
else if (ch=='P') cin >> value;
else if (ch=='T') value = ch;
else if (ch=='V') value = ch + 1000;
else value = 50;
cout << value << '\n';
}

Question 08: Re-write the program given in the question to use multi-way if-else statement instead of
switch statement?

#include<iostream>
using namespace std;
int main(){
int value;
char ch;
cin >> ch;
if (ch=='A'){
cout << ch << '\n';
value = 10;
}
else if (ch=='P' || ch=='E') cin >> value;
else if (ch=='T'){
cin >> ch;
value = ch;
cout << "Value = " << value << ", ch = " << ch << '\n';
}
else if (ch=='C'){
value = ch;
cout << "Value = " << value << " , ch = " << ch << '\n';
}
else if (ch=='V'){
value = ch + 1000;
cout << value << '\n';
}

Question 09: Rewrite the following code fragment so a while loop is used instead of for statement.

for (int i = 100; i > 0; i--)


std::cout << i << '\n';

Answer:

int i = 100;
while (i >= 1){
cout << i << ‘\n’;
i--;
}

Question 10: Rewrite the following code fragment so that it uses the conditional operator instead of an
if statement:

if (value % 2 != 0) // Is value even?


value = value + 1; // If not, make it even.

Answer:

value = (value % 2 != 0) ? value + 1: ;

Question 11: Rewrite the following code fragment so that it uses the conditional operator instead of an
if/else statement:

if (value % 2 == 0) // Is value even?


value = 0; // If so, make it zero.
else
value = value + 1; // Otherwise, make it even.

Answer:

(value % 2 == 0) ? 0 : value + 1;

Question 12: Would the following multi-way if/else be a good candidate to rewrite as a switch
statement? If so, rewrite the code using a switch; otherwise, explain why it is impractical to do so.

int x, y;
std::cin >> x >> y;
if (x < 10)
y = 10;
else if (x == 5)
y = 5;
else if (x == y)
y = 0;
else if (y > 10)
x = 10;
else
x = y;

It is impractical to do so because the above program includes less than (<) and greater than (>) operator
which cannot be used in switch statement. So, when we have to compare values it is highly
recommended to use if-else statements and switch statement isn’t applicable in such conditions and
circumstances.

You might also like