Revision 2
1. What is the output of the following program if ftemp=212.
// fahren.cpp
// demonstrates cin, newline
#include <iostream>
using namespace std;
int main()
{
int ftemp; //for temperature in fahrenheit
cout << " Enter temperature in fahrenheit: ";
cin >> ftemp;
int ctemp;
ctemp = (ftemp - 32) * 5 / 9;
cout << "Equivalent in Celsius is: " << ctemp << " \n ";
return 0;
}
2. What the following program does without running it?
#include <iostream>
using namespace std;
int main( ) {
int x = 5;
int y = 7;
cout << x + y << “ ” << x * y;
cout << endl;
return 0;
}
3. What should be corrected in the following program?
include <iostream>
using namespace std;
int main( ) {
cout << "Hello World \n";
return 0;
}
4. What is the output of: cout << “\”Run, Spot, run,\” she said.”;
5. What is the difference between:
cout << “x+y”; and cout << x+y ;
cout<<3 + 4 * 5; and cout<<(3 + 4) * 5
6. What is the output of:
a)#include <iostream>
using namespace std;
int main()
{
cout << 6 % 8 << endl
<< 7 % 8 << endl
<< 8 % 8 << endl
<< 9 % 8 << endl
<< 10 % 8 << endl;
return 0;
}
b) cout << 3 / 2 + 5.5;
#include <iostream>
using namespace std;
int main() { int a = 5;
cout << a++ ; int b = a ;
cout << endl; cout << b ; return 0;}
a) 5 b) 6 c) 5
5 6 6
7. What should be corrected in the following?
a. if score >= 60
cout << “passing” << endl;
b. #include <iostream>;
using namespace std
int main()
{
int x;
cout >> "Enter an integer number: ";
cin << x;
if( x > 100 );
cout << "This number is greater than 100\n";
return 0
};
8. True or false:
The expression in the if statement:
if (score = 30) grade = 'A';
always evaluates to true.
9. Circle the best answer.
a. if (60 <= 12 * 5)
cout << "Hello";
cout << " There";
outputs the following:
(i) Hello There (ii) Hello (iii) Hello (iv) There
There
b. if (7 <= 7) cout << 6 - 9 * 2 / 6 << endl;
outputs the following:
(i) -1 (ii) 3 (iii) 3.0 (iv) none of these
C. if (7 < 8){
cout << "2 4 6 8" << endl;
cout << "1 3 5 7" << endl; }
outputs the following:
(i) 2 4 6 8 (ii) 1 3 5 7 (iii) none of these
1357
d. if (5 < 3) cout << " *";
else if (7 == 8) cout << "&";
else
cout << "$";
outputs the following:
(i) * (ii) & (iii) $ (iv) none of these
e. #include <iostream>
using namespace std;
int main() {
int i = 5;
if (i < 55 % 5)
cout << "C++";
cout<<" Lang."; return 0; }
outputs the following:
a) C++ b) C++ Lang. c) Lang.
f. #include <iostream>
using namespace std;
double fun( double x, double y) {
if (x >= y) return x;
return y; }
int main(){ cout<< fun(5, 10); return 0;}
outputs the following:
a) 10 b) 5 c) None of the above.
g. #include <iostream>
using namespace std;
int main() {
int score = 78;
if (score = 50)
cout << "ok ";
cout << "*****"; return 0;}
a) ok ***** b) ***** c) ok
h. #include <iostream>
using namespace std;
int main() {
double g = 1.9;
if (g>= 2.0)
{ if (g >= 3.7) cout <<"The list"; }
else cout<<" No "; return 0;}
a)No b) The list c) None of the above.
10.Correct this code to print the correct message:
if (score >= 60)
cout << "You pass." << endl;
else;
cout << "You fail." << endl;
11.What is the output of the following C++ code?
x = 100;
y = 200;
if (x > 100 && y <= 200)
cout << x << " " << y << " " << x + y << endl;
else
cout << x << " " << y << " " << 2 * x - y << endl;
12.What is the output of this program?
a) #include <iostream>
using namespace std;
int main()
{
int i = 20; //wrong initialization
while (i < 20) //initially the loop entry condition evaluates to false
{ //the body of the while loop will never execute
cout << i << " "; //there won’t be any output
i = i + 5; //the value of i remains 20
}
cout << “**********”<<endl; //the first statement that will be executed
return 0;
}
b) #include <iostream>
using namespace std;
int main() { int i = 11;
do { cout << i; i = i + 5; }
while (i <= 10);
cout<<i; return 0; }
a) 1011 b) 1116 c) None of the above.
13.What is the output of the following C++ code?
a) int count = 1;
int y = 100;
while (count < 100){
y = y - 1;
count++; }
cout<<"y ="<<y<<"and count ="<<count<<endl;
b) Suppose that the input is:
58 23 46 75 98 150 12 176 145 -999
What is the output of the following program?
#include <iostream>
using namespace std;
int main() {
int num;
cout<<“enter a num”; cin >> num;
while (num != -999) {
cout << num % 25 << " ";
cin >> num; }
cout << endl;
return 0;}
c) #include <iostream>
using namespace std;
int main() {
int x, y, z;
x = 4; y = 5;
z = y + 6;
while (((z - x) % 4) != 0) {
cout << z << " ";
z = z + 7; }
cout << endl;
return 0; }
d) int num = 5;
while (num > 5)
num = num + 2;
cout << num << endl;
e) int num = 1;
while (num < 10){
cout << num << " ";
num = num + 2; }
cout << endl;
f. #include <iostream>
using namespace std;
int main(){
for (int i = 1; ; i++) cout << i;
cout << endl; return 0; }
a) infinite loop b) the output is nothing c) None of the above.
14.True or False
The following while loop terminates if j > 10.
j = 0;
while (j <= 10)
j++;
15.Correct the following code so that it finds the sum of 20 numbers.
sum = 0;
while (count < 20)
cin >> num;
sum = sum + num;
count++;
16.Consider the following function definition:
double func(double x, int y, string name)
{ //function body }
Which of the following is the correct function prototype?
i. double func();
ii. double func(double, int, string);
iii. double func(double x, int y, string name)
iv. func(double x, int y, string name);
17.Consider the following statements:
double num1, num2, num3;
int int1, int2, int3, value;
num1 = 5.0; num2 = 6.0; num3 = 3.0;
int1 = 4; int2 = 7; int3 = 8;
and the function prototype:
double cube (double a, double b, double c);
Which of the following statements are valid?
a. value = cube (num1, 15.0, num3);
b. cout << cube(num1, num3, num2) << endl;
c. cout << cube(6.0, 8.0, 10.5) << endl;
d. cout << cube(num1, num3) << endl;
e. value = cube(num1, int2, num3);
18.Consider the following function:
int secret (int x){
int i, j;
i = 2 * x;
if (i > 10)
j = x / 2;
else
j = x / 3;
return j - 1; }
int another (int a, int b){
int i, j;
j = 0;
for (i = a; i <= b; i++)
j = j + i;
return j; }
Assume that x, y, and k are int variables,
What is the output of:
a. x = 10; cout << secret(x) << endl;
b. x = 5; y = 8; cout << another(x, y) << endl;
c. x = 10; k = secret(x);
cout<<x<<" "<<k<<" "<< another(x, k)<<endl;
d. x = 5; y = 8; cout << another(y, x) << endl;
19.What is the output of this program:
a) #include <iostream>
using namespace std;
double larger (double x, double y);
int main() {
double num, max;
cout << "Enter 10 numbers." << endl;
cin >> num;
max = num;
for (int count = 1; count < 10; count++)
{
cin >> num;
max = larger (max, num);
}
cout << "The largest number is " << max << endl;
return 0; } //end main
double larger (double x, double y)
{
if (x >= y) return x;
else return y;
}
b) #include <iostream>
using namespace std;
void fun1 (int& a){ a *= 5; cout << a ; }
int main() { int b = 1;
fun1 (b); cout << b ; return 0; }
a) 55 b) 15 c) 51
c) #include <iostream>
using namespace std;
void rep();
void rep(char);
void rep(char, int);
int main() {
rep();
rep('/', 5); return 0; }
void rep (char ch, int n)
{for (int j=0; j<n; j++) cout << ch; }
void rep (char ch)
{for (int j=0; j<5; j++) cout << ch; }
void rep()
{for(int j=0; j<5; j++) cout << '*'; }
a) *****///// b) ***** c) /////*****
////
20.Consider the following program:
#include <iostream>
using namespace std;
void func1();
void func2();
int main(){
int num;
cout << "Enter 1 or 2: ";
cin >> num;
cout << “\n Take ";
if (num == 1) func1();
else if (num == 2) func2();
else
cout<<"Invalid input. You must enter1or2 \n";
return 0; }
void func1() {cout << "Programming I." <<endl;}
void func2(){cout << "Programming II." << endl;}
a. What is the output if the input is 1?
b.What is the output if the input is 2?
c. What is the output if the input is 3?
d.What is the output if the input is -1?
21.What is the output of the following program?
a. #include <iostream>
using namespace std;
void tryMe (int& v);
int main()
{
int x = 8;
for (int count = 1; count < 5; count++)
tryMe(x);
return 0;
}
void tryMe (int& v)
{
static int num = 2;
if (v % 2 == 0)
{ num++; v = v + 3; }
else
{num--; v = v + 5; }
cout << v << ", " << num << endl;
}
b. #include <iostream>
using namespace std;
void func1 () {
static int x=1;
x = x * 6;
cout << x ; }
int main(){ func1 (); func1 (); return 0;}
a) 636 b)66 c) None of the above.
22.Consider the following recursive function:
void myfunction (int x) {
if (x > 10){
myfunction (x / 10);
cout << x % 10 << endl;
}
else cout << x << endl; }
What is the output of the following statements?
myfunction(7);
myfunction(36);
23.Consider the following recursive function:
void myfunction (int x) {
if (x > 0 && x < 10) {
cout << x << " ";
myfunction(x + 1);
}}
What is the output of the following statements?
myfunction(0);
myfunction(5);
#include <iostream>
using namespace std;
int fun (int m, int n) {
if (n== 0) return 0;
else if (n ==1) return m;
else return (m + fun (m, n-1)); }
int main(){ cout<<fun (12, 2); return 0;}
a) 2 b) 12 c) 24 d) none of the above.
24.Consider the following recursive function:
int myfunction (int x) {
if (x == 0) return 2;
else if (x == 1) return 3;
else return (myfunction (x - 1) + myfunction (x - 2)); }
What is the output of the following statements?
a. cout << myfunction (0) << endl;
b. cout << myfunction (1) << endl;
c. cout << myfunction (2) << endl;
25.What is the output of the following program segment?
a. int temp[5];
for (int i = 0; i < 5; i++)
temp[i] = 2 * i - 3;
for (int i = 0; i < 5; i++)
cout << temp[i] << " ";
cout << endl;
temp[0] = temp[4];
temp[4] = temp[1];
temp[2] = temp[3] + temp[0];
for (int i = 0; i < 5; i++)
cout << temp[i] << " ";
cout << endl;
b. #include <iostream>
using namespace std;
int main() {
int arr[10]={10}; cout<< arr [1]; return 0;}
a) 10 b) 0 c) None of the above.
26.What is stored in the array list after the following C++ code executes?
list[0] = 5;
for (int i = 1; i < 6; i++)
{
list[i] = i * i + 5;
if (i > 2)
list[i] = 2 * list[i] - list[i - 1];
}
27.Write C++ statements to define and initialize the following arrays.
a. Array heights of 10 components of type double. Initilaize this array to the
following
values: 5.2, 6.3, 5.8, 4.9, 5.2, 5.7, 6.7, 7.1, 5.10, 6.0.
b. Array weights of 7 components of type int. Initialize this array to the
following values:
120, 125, 137, 140, 150, 180, 210.
c. double x[10][20];
a) declares the two dimensional array x of 10 rows and 20 columns, in which
every element is of type double
b) declares the two dimensional array x of 10 columns and 20 rows, in which
every element is of
type double
c) None of the above.
28.Consider this function heading: void tryMe(int x[], int size);
and these declarations:
int list[100];
int score[50];
double gpas[50];
Which of the following function calls is valid?
a. tryMe(list, 100);
b. tryMe(list, 75);
c. tryMe(score, 100);
d. tryMe(score, 49);
e. tryMe(gpa, 50);
29.What is the output of the following C++ code?
double salary[5] = {25000, 36500, 85000, 62500, 97000};
double rate = 0.03;
for (int i = 0; i < 5; i++)
cout << (i + 1) << " " << salary[i] << " “ << salary[i] * rate << endl;
30.What is the output of the following program?
#include <iostream>
using namespace std;
int main(){
int count;
int alpha[5];
alpha[0] = 5;
for (count = 1; count < 5; count++) {
alpha[count] = 5 * count + 10;
alpha[count - 1] = alpha[count] - 4;
cout << "List elements: ";
for (count = 0; count < 5; count++)
cout << alpha[count] << " ";
cout << endl;
return 0;
}
Pointer
31. Initialize the pointer ptr so that it points to nothing.
a) ptr = NOTHING; b) ptr = 0; c) None of the above
32. To de-allocate the dynamic array p:
a) delete []p ; b) delete p ; c) None of the above
33.To declare x to be a pointer to a pointer:
a) int *x ; b) int **x ; c) None of the above
34.Make the pointers p1 and p2 point to the same memory location.
a) p1 == p2 b) p1= p2 c) None of the above
35.What is the output of this program?
int a = 5, b = 10, c = 15;
int *arr[ ] = {&a, &b, &c};
cout<< arr[1];
36.Choose the right option string* x, y;
a) x is a pointer to a string, y is a string
b) y is a pointer to a string, x is a string
c) both x and y are pointers to string types
d) none of the mentioned
37. What is the output of this program?
char *ptr;
char Str[] = "abcdefg";
ptr = Str;
ptr += 5;
cout << *ptr<<endl;
38.Which of the following access specifies is used in a class definition by
default?
a) Protected
b) Public
c) Private
d) Friend
39.What will happen in this code?
int a = 100, b = 200;
int *p = &a, *q = &b;
p = q;
a) b is assigned to a
b) p now points to b
c) a is assigned to b
d) q now points to a
40.What is the output of this code?
int arr[20];
int i;
for(i = 0; i < 10; i++)
*(arr + i) = 2 + i;
cout << *(arr+3);
41.What is the output of the following program?
using namespace std;
main() {
int a[] = {10, 20, 30};
cout<<*a+1;
}
42.What is the output produced by the following code?
int *p1, *p2;
p1 = new int;
p2 = new int;
*p1 = 10;
*p2 = 20;
cout << *p1 << " " << *p2 << endl;
p1 = p2;
cout << *p1 << " " << *p2 << endl;
*p1 = 30;
cout << *p1 << " " << *p2 << endl;
How would the output change if you were to replace
*p1 = 30;
with the following?
*p2 = 30;
43.What is the output of the following code fragment? The code is assumed to
be embedded in a correct and complete program.
int a[10];
int *p = a;
int i;
for (i = 0; i < 10; i++)
a[i] = i;
for (i = 0; i < 10; i++)
cout << p[i] << " ";
cout << endl;
44.What is the output of the following code fragment? The code is assumed to
be embedded in a correct and complete program.
int array_size = 10;
int *a;
a = new int[array_size];
int *p = a;
int i;
for (i = 0; i < array_size; i++)
a[i] = i;
p[0] = 10;
for (i = 0; i < array_size; i++)
cout << a[i] << " ";
cout << endl;
41.#include <iostream>
using namespace std;
int main() { int x = 1;
int *p = &x;
*p = 24;
x = x + 10; cout << x ; return 0;}
a) 34 b) 11 c) None of the above.
42. Based on the previous point, choose the best answer.
cout << p ; return 0;}
a) Address of p b) Address of x c) None of the above.
43) Based on point 41, choose the best result to this code:
p++;
a) Increases the value of p by one
b) Increases the value of p by 4 bytes
c) None of the above.
44. Choose what must be corrected for each of the following:
1) #include <iostream>
int main() {
std..cout <<100 * 0.25 ; endl; return 0; }
a) <<100 * 0.25 ; endl; b) std..cout c) All of the above.
2) #include <iostream>
#include <cmath> using namespace std;
int main() { cout<<sqrt(9.0); return 0;}
a) cout<<sqrt(9.0);
b)#include <cmath> using namespace std;
c)None of the above.
3) structType s1, s2;
structType students[100];
s1== s2;
a) s1== s2; b) structType students[100]; c) None of the above.
4) int n[10]={1, 3, 5, , 9};
int i;
for (i = 9; i > 0; i--) cout<< n[i] ;
a) int n[10]={1, 3, 5, , 9}; b) i > 0; c) All of the above.
5) int fun (const int x[], int xSize){
for (int i = 0; i < xSize; i++)
cout << x[i] ; }
a) int fun b) const int x[] c) All of the above.
6) class die {
int n=0;
public:
int getN() const;
void die();
int roll(); }
a) void die(); b) int n=0; c) None of the above. d) All of the above.
7) Based on the previous point, choose the
what must be corrected.
a) int getN() const; b) int roll(); } c) None of the above. d)All of the above.
8) void print(studentType s)
{cout<<name; cout<<score;}
a) void print(studentType s)
b){cout<<name; cout<<score;}
c) None of the above
9) struct empType {
double expyears;
int sal; } empType emp;
a) int sal; } empType emp;
b) struct empType {
c)None of the above
10) int *intList, int size;
cout<<"Enter the size "; cin >> size;
intList = new [size];
a) intList = new [size];
b) int *intList, int size;
c) All of the above.
11) void i(empType& e)
{ e::sal = 0; e::expyears = 3.5; }
a) {e::sal = 0; e::expyears =3.5;}
b) void i(empType& e)
c)None of the above
What are the disadvantages of arrays?
a) We must know beforehand how many elements will be there in the array
b) There are chances of wastage of memory space if elements inserted in an array
are lesser than the allocated size
c) Insertion and deletion becomes tedious
d) All of the mentioned
What are the advantages of arrays?
a) Easier to store elements of same data type
b) Used to implement other data structures like stack and queue
c) Convenient way to represent matrices as a 2D array
d) All of the mentioned
Assuming int is of 4bytes, what is the size of int arr[15]?
a) 15
b) 19
c) 11
d) 60
Can we change the size of an array at run time?
What is the output of the following program statements?
int count = 10, *temp, sum = 0;
temp = &count;
*temp = 20;
temp = ∑
*temp = count;
cout << "count = " << count << endl;
cout << "*temp = " << *temp << endl;
cout << "sum = " << sum << endl;
2. What will be used when terminating a structure?
a) :
b) }
c) ;
d) ;;
Which of the following is the address operator?
a. @
b. #
c. &
d. %
Static Variables
Q: Trace the following programs:
a)
void counter()
{ int count=0; cout << count++; }
int main()
{
for(int i=0;i<5;i++)
counter();
}
0 0 0 0b)
void counter()
{
static int count=0;
cout << count++;
}
int main()
{
for(int i=0;i<5;i++)
counter();
}