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

Final Part One

This document contains a test with various question types including fill-in-the-blank, multiple choice, true-false, and matching. It provides explanations for the true-false questions and discusses evaluating multiple answers for multiple choice questions. There is also one free form question about rewriting a while loop as a for loop.

Uploaded by

VishavjeetDevgan
Copyright
© © All Rights Reserved
0% found this document useful (0 votes)
86 views

Final Part One

This document contains a test with various question types including fill-in-the-blank, multiple choice, true-false, and matching. It provides explanations for the true-false questions and discusses evaluating multiple answers for multiple choice questions. There is also one free form question about rewriting a while loop as a for loop.

Uploaded by

VishavjeetDevgan
Copyright
© © All Rights Reserved
You are on page 1/ 14

These test questions are fill in the blank, multiple choice, and true-false.

The multiple choice


questions may have more than one correct answer. There is one matching question. Mark all of
the correct answers for full credit.
True False questions require an explanation in addition to the true/false response, and, if false,
also require a correction.

True False:

 In C++ you can assign an expression of type double to a variable of type int with no problem.
Answer: TRue
Explanation:

 If we execute the code fragment in an otherwise complete, correct program:


n = 1;
cout << n++ << " " << n++ << " " << n++ << endl;
the output is guaranteed to be 1 2 3.

Answer:NO

Explanation:
if

n=1

n=(n++) + (n++) will print 3 after second line is executed as first n++ will be executed after +
operator, and second n++ operator will be executed after the statement is executed.

n=1+2=3

so the correct statement is this

 Given the declaration

int x = 0;
The following expression causes a divide by zero error:

(x !=0) || (2/x < 1);

Answer:

Explanation:
 In a do-while loop, a continue statement terminates the loop.
Answer: False

Explanation:  The boolean expression is executed before entering the body of the loop

 It is legal to replace the prototype

double totalCost(int numberParam, double priceParam);

with the more terse, alternate form

double totalCost(int, double);

Answer: True

prototypes can be duplicated.

Explanation:

 When using an array, it is perfectly legal to access indexed variables with index values less
than 0 or greater than or equal to the declared size of the array.

Answer:

Explanation:
 Consider these hierarchical structures.
struct Date
{
int year;
//members
};
struct Person
{
Date birthDay;
//other members
};
Person Bill;

The year of Bill’s birthday may be accessed as Bill.year;


Answer:

#include <iostream>

using namespace std;

struct Date{

int month, day, year;

};

struct Person{

string name;

Date date;

};

bool checkLeapYear(int year){


if(year<0){

return false;

else if(year % 400 == 0){

return true;

else if(year % 100 == 0){

return false;

else if(year % 4 == 0){

return true;

return false;

int main()

struct Person p1, p2;

cin>>p1.name>>p1.date.month>>p1.date.day>>p1.date.year;

cin>>p2.name>>p2.date.month>>p2.date.day>>p2.date.year;

cout<<p1.name<<"'s birthday is";

if(checkLeapYear(p1.date.year)){

cout<<" a leap year."<<endl;

else{
cout<<" a not leap year."<<endl;

cout<<p2.name<<"'s birthday is";

if(checkLeapYear(p2.date.year)){

cout<<" a leap year."<<endl;

else{

cout<<" a not leap year."<<endl;

return 0;

Explanation:

 A class may not have another class type object as a member.


Answer:
Explanation:

 The declaration below declares three pointer variables of type pointer to double that is, a
pointer of type (double*)

double* p1, p2, p3;


Answer: is wrong

Explanation: In above code p1 is a pointer where as p2 and p3 are data type double
variable.
Multiple Choice

 Pick the C++ keywords out of the following list.


a) while
b) total_weight
c) double
d) if
e) number_of_bars

Answer: Keywords are


 The value of the expression 20.0 * (9/5) + 32.0 is
a) 68.0
b) 52.0
c) incorrect expression so there is no value
d) 32.0
e) incorrect expression , the / should be %
Answer:

 Which of the following is true of the && operator?


a) It has two operands.
b) It can have one operand.
c) It uses short circuit evaluation.
d) It is the logical AND operator.
e) It returns true if either operand is true.
Answer:

 The statements int x = 1; int y; y = x++;


a) Assign y the value 2;
b) Change the value of x to 2.
c) Assign y the value 0;
d) Assign y the value 1;
e) The ++ is a postfix operator.
Answer:

 A void function can have


>>”void” is wrong font.
a) no arguments
b) as many arguments as the programmer wishes
c) no more than 3 arguments
d) exactly one argument
Answer:

 Given the function definition, which of the following are correct?


int func(int n, double d)

int j = n;

double sum = 0;
while( j >= 0)

sum += d;
-j;

return sum;

With arguments 7and 2.0

a) returns 7*2
b) returns 7+2
c) returns 7!
d) There is a syntax error in the program so it won’t run.
e) It compiles but computes none of these.
Answer:
 Given the function, and the main function calling it: What is the output of the following code if you
omit the ampersand (&) from the first parameter, but not from the second parameter? (You are to
assume this code is embedded in a correct function that calls it.):
#include <iostream>

using namespace std;

void func(int & x, int & y)

int t = x;

x = y;

y = t;

int main()

int u = 3; v = 4;

// ...

cout << u << " " << v << endl;

func ( u, v )

cout << u << " " << v << endl;

// ...

a) 3 4

33
b) 3 4
43

c) 3 4

34
d) 3 4

44
e) none of the above. If you choose this, you must specify the output.

Answer: B

 Are the following array initializations correct? If not, why not?


a) int x[4] = {8, 7, 6, 5, 4};
b) int x[] = {8, 7, 6, 5, 4};
c) int x[4] = {8, 7, 6};
d) const int SIZE =4;
int x[SIZE];

e) const int SIZE =4;


int x[SIZE-4];

Answer: A is incorrect

 When you define a C++ class, which of the following should be part of the implementation?
a) all declarations of private member variables
b) all declarations for public member functions
c) all explanatory comments for public member declarations.
d) all declarations for private member functions (auxiliary or helping functions)
e) all member function definitions, public and private (implementations of functions).
Answer:
 Given the class definition,
class A
{
public:
A(){}
A(int x, char y):xx(x), yy(y) {}
// other members
private:
int xx;
char yy;
};
Tell which definition below is legal.
If legal, tell whether it is a definition of an object of class A.
If the definition is a legal and defines a class A object, tell which constructor is called for
each of the following definitions.
Identify the constructor like this: If the constructor for class A with two int arguments is
called, respond with A(int, int).
a) A x(2, ‘A’);
b) A x;
c) A x = A(2, ‘A’);
d) A x(1);
e) A x( );
Answer: A is correct
Free Form Questions:
 What does the line
#include <iostream>
do for your program?
Answer:

 Rewrite the following while loops as for loops:


int i = 1;
while(i<=10)

{
if (i<5 && i !=2)

cout << 'X';

i++;

}
Answr
for(i=1;i<=10;i++)
{
If (I < 5 && i != 2)
cout <<

 What is the error in the following code? Why is this wrong?


int f(int x)

int x;

// code for function body

Answer:
 Define function signature:
Answer:

 Describe the action of the new operator. What does the new operator return? What are the
indications of error?
Answer:

 Answer these questions about destructors


a) What is a destructor and what must the name of a destructor be?
b) When is a destructor called?
c) What does a destructor actually do?
d) What should a destructor do?
Answer:

You might also like