In: Computer Science
A1.State whether each of the following is true or false. If false, explain why:
a.In C++ the operator for raising to a power has lower precedence than the multiplication operator
b.In C++ the statement
x = y = x = 0;
is illegal.
c.In C++ the expression
x < y < z
is illegal.
d.In C++ the expression
x <= y = z
is legal.
A2.Consider the following program:
void main() {
enum direction { EAST, WEST, NORTH, SOUTH };
direction dir = NORTH;
int num = 2;
s1: num = dir;
s2: dir = (direction) num;
}
Why a type cast is not necessary in the statement labelled s1, but
it is necessary in the statement labelled s2?
A3.List two advantages of using named constants as opposed to literals
A4.What is the purpose of a function prototype?
A5.Why is it necessary to double the backslash character in a string literal?
A6.In the table below fill in the type and the fixed format value of each of the listed literals. If it is not a valid literal, fill in “INVALID” for the type.
TYPE || Value in fixed format
2.5E2
2.5F2
2E2
2E2F
2F2E
-2E-2F
A7.What is the output of the following code snippet?
int num = 0;
int* ptr = #
num = 5;
*ptr = *ptr + 5;
cout << num << " " << *ptr << endl;
a)5 5
b)5 10
c)10 5
d)10 10
A8.What will be printed by the following
function:
void printBalance()
{
int balance=90;
while (!(balance < 9))
balance -= 9;
cout << balance << end1;
}
A9.Answer True or False
a) The expression:
!(amount > 100 && amount <= 200)
always has the same Boolean value as the expression
amount <= 100 || amount > 200
b) The expression:
!(amount > 200 || amount <= 100)
always has the same Boolean value as the expression
amount > 100 && amount <= 200
A10.Show the output of the following program in the
box on the right:
#include <iostream>
using namespace std;
int getTotal();
int main()
{
cout << "Total is " << getTotal();
system("pause");
}
int getTotal(){
int x, y, total = 0;
x = 1;
while (x < 6){
y = x*x;
cout << y << endl;
total = total + y;
++x;
}
return total;
}
A11.What is the error in the following code
snippet?
[2 points]
int x = 3;
int y = 3;
switch (x+3){
case 3: y = 3;
break;
case 6: y = 1;
default: y += 1;
}
int * ptr;
* ptr = y;
cout << &ptr << " " << *ptr <<
end1;
A12.What is the error in the following code snippet?
#include <iostream>
using namespace std;
void main() {
for (int cnt = 1; ; cnt++){
int * a = new int;
*a = cnt;
if (cnt >= 5) return;
double d = 1.0;
d -= 1.5 * 3 + * a++;
cout<< d << end1;
system("pause");
}
}
State whether each of the following is true or false.
If false, explain why:
a.In C++ the operator for raising to a power has lower precedence
than the multiplication operator
b.In C++ the statement
x = y = x = 0;
is illegal.
c.In C++ the expression
x < y < z
is illegal.
d.In C++ the expression
x <= y = z
is legal.
1.
a) False : Exponents or powers have a higher precedence over multiplication/ division.
b) False: x=y=x=0 is a legal statement in C++ it both x and y have been declared previously.
c) False : x < y < z is a legal statement but it will work like this
(x<y) < z
d) True : x <= y = z is an illegal statement because <= is a comparison operator and = is a assignment operator.
4. Function Prototype
5. It is necessary to double the backslash character in string literal because C++ assigns a special meaning to a single backslash hence it is not read as a character inside the string. To avoid that, two backslashes are used.
7. Output for the code:
int num = 0;
int* ptr = #
num = 5;
*ptr = *ptr + 5;
cout << num << " " << *ptr << endl;
is 10,10(d)
this is because ptr is a pointer and is storing the memory address of the num variable
so whenever a change is made to *ptr it will be reflected to the variable also. Hence, num changed from 5 to 10 and the output was 10 10.
Please upvote if my answer helped. Thanks and stay safe.