Q(1) : Which of the following data types can be used to represent integers? a.
char b.
long c.
short d.
All of the above.
ANS: d. All of the above.
Q(2) What will the following program segment do? int counter = 1; do
{
cout << counter << " ";
} while ( ++counter <= 10 ); a.
Print the numbers 1 through 11 . b.
Print the numbers 1 through 10 . c.
Print the numbers 1 through 9 . d.
Cause a syntax error.
ANS: b. Print the numbers 1 through 10 .
Q(3) : In C++, the condition ( 4 > y > 1 ) : a.
Evaluates correctly and could be replaced by ( 4 > y && y > 1 ). b.
Does not evaluate correctly and should be replaced by ( 4 > y && y > 1 ). c.
Evaluates correctly and could not be replaced by ( 4 > y && y > 1 ). d.
Does not evaluate correctly and should not be replaced by ( 4 > y && y > 1 ).
ANS: b. Does not evaluate correctly and should be replaced by ( 4 > y && y > 1 ).
Q(4) : switch can be used to test: a.
int constants.
b.
float constants.
c.
string constants.
d.
all types of constants.
ANS: a. int constants.
Q(5) : Functions can: a.
Be used as building blocks to create new programs. b.
Return a result to the caller function. c.
Be reused any number of times. d.
Do all of the above.
ANS d. Do all of the above.
1
Q(6) : Which of the following is not included in <cmath> ? a.
pow . b.
floor . c.
ln . d.
log .
ANS c. ln .
Q(7) : The function prototype
double mySqrt( int x ); a.
Declares a function called mySqrt which takes an integer as an argument and returns a double . b.
Defines a function called double which calculates square roots. c.
Defines a function called mySqrt which takes an argument of type x and returns a double . d.
Declares a function called mySqrt which takes a double as an argument and returns an integer.
ANS: a. Declares a function called mySqrt which takes an integer as an argument and returns a double .
Q(8) : The argument list of a function call must match, or be consistent with, the parameter list of the called function in all of the following details, except: a.
The number of arguments/parameters in the list. b.
The types of arguments/parameters in the list. c.
The names of arguments/parameters in the list. d.
The argument list and parameter list must match in all of the above details.
ANS: c. The names of arguments/parameters in the list.
Q(9): A function prototype does not have to: a.
Include parameter names. b.
Terminate with a semicolon. c.
Agree with the function definition. d.
Match with all calls to the function.
ANS: a. Include parameter names.
Q10 : A function prototype can always be omitted when: a.
A function is defined before it is first invoked. b.
A function is invoked before it is first defined. c.
A function takes no arguments. d.
A function does not return a value.
ANS: a. A function is defined before it is first invoked.
Q11: Which of the following is not a valid enumeration statement? a.
Enum person { me, you, them }; . b.
Enum person { me = 1, you = 2, them = 3 }; . c.
Enum person { me = 0, you = 0, them = 0 }; .
2
d.
Enum person { me, you, me }; .
ANS: d. Enum person { me, you, me }; .
Q12: Which of the following is false about the following function prototype? void functionA( void ); a.
It does not receive any arguments. b.
It could have been written void functionA( ); . c.
It does not return a value.
d.
It could have been written functionA( void ); .
ANS: d. It could have been written functionA( void ); .
Q13: The inline keyword: a.
Increases function-call overhead. b.
Can reduce a function’s execution time but increase program size. c.
Can decrease program size but increase the function’s execution time.
d.
Should be used with all frequently used functions.
ANS: b. Can reduce a function’s execution time but increase program size.
Q14: When an argument is passed-by-value, changes in the called function __________ affect the original variable’s value; when an argument is passed call-by-reference, changes in the called function __________ affect the original variable’s value.
a.
Do not, do. b.
Do not, do not. c.
Do, do.
d.
Do, do not.
ANS: a. Do not, do.
Q15: A reference parameter: a.
Is an alias for its corresponding argument. b.
Is declared by following the parameter’s type in the function prototype by an ampersand
( & ). c.
Cannot be modified.
d.
Both (a) and (b).
ANS: d. Both (a) and (b).
Q16: If the function int volume( int x = 1, int y = 1, int z = 1 ); is called by the expression volume( 3 ) , how many default arguments are used?
a.
None. b.
One. c.
Two.
d.
Three.
ANS: c. Two.
3
Q17: The unary scope resolution operator is used: a.
To access a global variable when a local variable of the same name is in scope. b.
To access any variable in an outer block when a local variable of the same name is in scope. c.
To access a global variable when it is out of scope.
d.
To access a local variable with the same name as a global variable.
ANS: a. To access a global variable when a local variable of the same name is in scope.
Q(18) :Which of the following does the C++ compiler not examine in order to select the proper overloaded function to call?
a.
Types and order of the arguments in the function call. b.
The number of arguments in the function call. c.
The return type of the function.
d.
It examines all of the above.
ANS: c. The return type of the function.
Q19: Overloaded functions must have: a.
Different parameter lists. b.
Different return types. c.
The same number of parameters.
d.
The same number of default arguments.
ANS: a. Different parameter lists.
Q20: C++ functions other than main are executed: a.
Before main executes. b.
After main completes execution. c.
When they are explicitly called by another function. d.
Never.
ANS c. When they are explicitly called by another function.
Q21: Calling a member function of an object requires which item? a.
The dot operator. b.
Open and close braces. c.
The class name. d.
None of the above.
ANS a. The dot operator.
Q22: In the UML, the top compartment of the rectangle modeling a class contains: a.
The class’s name. b.
The class’s attributes. c.
The class’s behaviors. d.
All of the above.
ANS: a. The class’s name.
4
Q23: Two adjacent parameters are separated by what symbol? a.
Dot. b.
Comma. c.
Parentheses. d.
Braces.
ANS: b. Comma.
Q24: Attributes of a class are also known as: a.
Constructors. b.
Local variables. c.
Data members. d.
Classes.
ANS: c. Data members.
Q25: What is the default initial value of a String ? a.
"" b.
"default" c.
default d.
None of the above.
ANS: a. ""
Q26: A default constructor has how many parameters? a.
0. b.
1. c.
2. d.
Variable number.
ANS: a. 0.
Q27: A constructor can specify the return type: a.
int . b.
string . c.
void . d.
A constructor cannot specify a return type.
ANS: d. A constructor cannot specify a return type.
Q28: The compiler will implicitly create a default constructor if: a.
The class does not contain any data members. b.
The programmer specifically requests that the compiler do so. c.
The class does not define any constructors. d.
The class already defines a default constructor.
ANS: c. The class does not define any constructors.
5
Q29: To execute multiple statements when an if statement’s condition is true , enclose those statements in a pair of: a.
Parentheses, ( ) . b.
Square Brackets, [ ] . c.
Braces, { } . d.
Angle brackets, < > .
ANS: c. Braces, { } .
Q30: Assuming that the string object text contains the string " Hello!!!
", the "expression text.substr( 2 , 5 ) would return a string object containing the string: a.
" llo!!
". b.
" llo!
". c.
" ello!
". d.
" ello ".
ANS: a. " llo!!
".
Q31: Member access specifiers ( public and private ) can appear: a.
In any order and multiple times. b.
In any order ( public first or private first) but not multiple times. c.
In any order and multiple times, if they have brackets separating each type. d.
Outside a class definition.
ANS: a. In any order and multiple times.
Q32: Member function definitions: a.
Always require the scope resolution operator ( :: ). b.
Require the scope resolution operator only when being defined outside of the definition of their class. c.
Can use the scope resolution operator anywhere, but become public functions. d.
Must use the scope resolution operator in their function prototype.
ANS: b. Require the scope resolution operator only when being defined outside of the definition of their class.
Q33: Every object of the same class: a.
Gets a copy of every member function and member variable. b.
Gets a copy of every member variable. c.
Gets a copy of every member function. d.
Shares pointers to all member variables and member functions.
ANS: b. Gets a copy of every member variable.
6
Q34: Variables defined inside a member function of a class have: a.
File scope. b.
Class scope. c.
Block scope. d.
Class or block scope, depending on whether the binary scope resolution operator ( :: ) is used.
ANS: c. Block scope.
Q35: A class-scope variable hidden by a block-scope variable can be accessed by preceding the variable name with the class name followed by: a.
:: b.
: c.
.
d.
->
ANS: a. ::
Q36: A default constructor: a.
Is a constructor that must receive no arguments. b.
Is the constructor generated by the compiler when no constructor is provided by the programmer. c.
Does not perform any initialization. d.
Both (a) and (b).
ANS: d. Both (a) and (b).
Q(37)Assuming the following constructor is provided for class Time
Time( int = 0, int = 0, int = 0 ); which of the following is not a valid way to initialize a Time object? a.
Time t1; b.
Time t2{ 22, 40 }; c.
Time t3( 22, 40 ); d.
(a), (b) and (c) are all valid ways to initialize a Time object.
ANS: d. (a), (b) and (c) are all valid ways to initialize a Time object.
Q38: Which of the following is not true of a constructor and destructor of the same class? a.
They both have the same name aside from the tilde ( ~ ) character. b.
They are both usually called once per object created. c.
They both are able to have default arguments. d.
Both are called automatically, even if they are not explicitly defined in the class.
ANS: c. They both are able to have default arguments.
Q39: Which of the following is not true of a destructor? a.
It performs termination housekeeping. b.
It is called before the system reclaims the object’s memory.
7
c.
If the programmer does not explicitly provide a destructor, the compiler creates an “empty” destructor. d.
It releases the object’s memory.
ANS: d.
It releases the object’s memory
Q40: Given the class definition: class CreateDestroy
{ public:
CreateDestroy() { cout << "constructor called, "; }
~CreateDestroy() { cout << "destructor called, "; }
};
What will the following program output? int main()
{
CreateDestroy c1;
CreateDestroy c2;
return 0;
} a.
constructor called, destructor called, constructor called, destructor called, b.
constructor called, destructor called, c.
constructor called, constructor called, d.
constructor called, constructor called, destructor called, destructor called,
ANS: d. constructor called, constructor called, destructor called, destructor called,
Q41: Given the class definition: class CreateDestroy
{ public:
CreateDestroy() { cout << "constructor called, "; }
~CreateDestroy() { cout << "destructor called, "; }
};
What will the following program output?
8
int main()
{
for ( int i = 1; i <= 2; ++i )
CreateDestroy cd;
return 0;
} a.
constructor called, destructor called, constructor called, destructor called, b.
constructor called, constructor called, c.
constructor called, constructor called, destructor called, destructor called, d.
Nothing.
ANS: a. constructor called, destructor called, constructor called, destructor called,
Q(42 ) A class-scope variable hidden by a block-scope variable can be accessed by preceding the variable name with the class name followed by: a.
:: b.
: c.
.
d. ->
ANS: a. : :
Q(43 ) A default constructor: a.
Is a constructor that must receive no arguments. b.
Is the constructor generated by the compiler when no constructor is provided by the programmer. c.
Does not perform any initialization. d.
Both (a) and (b)
ANS: d.
Q( 44) static data members of a certain class: a.
Can be accessed only if an object of that class exists . b.
Cannot be changed, even by objects of the same that class .
*c. Have class scope and shared memory location for all objects . d.
Can only be changed by static member functions .
ANS: c.
Q(45 ) The delete [ ] operator: a.
Can terminate the program. b.
Must be told which destructor to call when destroying an object.
*c. Can delete an entire array of objects declared using new . d.
Is called implicitly at the end of a program.
ANS: c.
9
Q(46 ) A copy constructor: a.
Is a constructor with only default arguments.
*b. Is a constructor that initializes a newly declared object to the value of an existing object of the same class. c.
Is a constructor that takes no arguments. d.
None of the above.
ANS: b.
Q(47) A constructor function is executed automatically when a.an object is created. b. a class is created. c.class and object are created . d. a constructor function is invoked ( called).
ANS: a.
Q(48) :
Let class foo is defined as: int main()
{ foo f1, f2, f3 ;
return 0;
} a. All objects have own a static variable and share member functions. b. All objects share a static variable and share member functions. c. All objects have own a static variable and own member functions. d. All objects have not variable and share member function.
ANS: b.
10
Q(49) : A member function can always access the data a. In the object of which it is a member . b. In the class of which it is a member. c. In any object of the class of which it is a member. d. In the public part of its class.
ANS: b.
Q(49) : Every object of the same class a. Gets a copy of every member function and member variable . b. Gets a copy of every member variable and share member functions. c. Gets a copy of every member function and share every member variables. d. Shares pointers to all member variables and member functions
ANS: b.
Q(
50
) : Which statement would be used to declare a 10-element integer array c ? a.
array c = int[ 10 ]; b.
c = int[ 10 ]; c.
int array c[ 10 ]; d.
int c[ 10 ];
ANS: d.
Q(51) To use a predefined function in a program, you need to know only the name of the function and how to use it. a.True b.False
Q(51) A value-returning function returns only one value. a.True b.False
Q(52) Parameters allow you to use different values each time the function is called. a.True b.False
Q(53) When a
statement executes in a user-defined function, the function immediately exits. a.True b.False
Q(54) A value-returning function returns only integer values. a.True b.False
11
Q(55) Consider the following function definition: double func( double x, int y, string name)
{
//function body
}
Which of the following is the correct function prototype of the function func ? a.
double func();
*b . double func( double , int , string); c.
double func( double x, int y, string name) d.func( double x, int y, string name);
Ans: b
Q(56) Consider the following statements: double num1, num2, num3; int int1, int2, int3; int 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, num2, num3);
*b. cout << cube(num1, num3, num2) << endl; c. cube(6.0, 8.0, 10.5) ; d. cout << cube(num1, num3) << endl;
Ans: b
Q(57) Consider the following function: int mystery( int x, double y, char ch)
{
int u;
if ( x<= y )
return (2 * x + static_cast < int >(y));
else
return ( static_cast < int >(2 * y) - x);
}
What is the output of the following C++ statements? cout << mystery(5, 4.3, 'B') << endl; a. 9 b. 1 c. 8 d. Zero
Ans: a
12
Q(58) The following formula gives the distance between two integer points, (x
1
, y
1
) and (x
2
, y
2
) in the
Cartesian plane:
√( 𝑥
1
− 𝑥
2
)
Which function can represent distance: a) Int Distance ( int x1, int y1 , int x2 , int y2)
{ int xx= x1-x2 ;
Int yy= y1-y2; return sqrt( ( xx*xx ) + (yy*yy) ) ;
} b) double Distance ( int x1, int y1 , int x2 , int y2)
{ int xx= x1-x2 ;
Int yy= y1-y2; return sqrt ( xx*xx ) + (yy*yy) ;
} c) double Distance ( int x1, int y1 , int x2 , int y2)
{ int xx= x1-x2 ;
Int yy= y1-y2; return sqrt ( ( xx-yy)*(xx-yy) ) ;
} d) double Distance ( int x1, int y1 , int x2 , int y2)
{ int xx= x1-x2 ;
Int yy= y1-y2; return sqrt( xx*xx + yy*yy ) ;
}
Ans: d
Q(59) double rectangle( double L, double W)
2
+ ( 𝑦
1
{
return L * V;
}
The return value of calling function rectangle(2.0 , 3.0 ) is
6
5
6.0
Error
Ans: d
− 𝑦
2
)
2
13
Q(60)
3 is: a) double Volume ( double r)
{ const double Pi=3.1415; return 4/3*Pi*r*3 ;
} b) double Volume ( double r)
{ const double Pi=3.1415; return 4/3*Pi*r*r*r ;
} c) double Volume ( double r)
{ const double Pi=3.1415; return 4.0/3.0*Pi*r*r*r ;
} d) double Volume ( double r)
{ const int Pi=3.1415; return 4/3*Pi*r*r*r ;
}
Ans: c
Q(61) If a C++ function does not use parameters, parentheses around the empty parameter list are still required..
a.
True b.
False.
Ans: a. True
Q(62) In C++, the names of the corresponding formal and actual parameters must be the same. a.
True b.
False.
Ans: b. False
Q(63) In C++, function definitions can be nested; that is, the definition of one function can be enclosed in the body of another function.
a.
True b.
False.
Ans: b. False
Q(64) Using global variables in a program is a better programming style than
14
using local variables, because extra variables can be avoided.
a.
True b.
False.
Ans: a. True
Q(65) The memory for a static variable remains allocated between function calls. a.
True b.
False.
Ans: a. True
Q(66) The memory for a variable deallocated between function calls , This is a------------ a. Parameter variable b.Static Variable c.Global variable d.Automatic variable
Ans: d. Automatic
Q(67) The memory for a variable allocated between function calls , This is a------------- a. Parameter variable b.Static Variable c.Global variable d.Automatic variable
Ans: b. Static
Q(68) Whenever the value of a parameter changes, the value of the argument changes.
The parameter is a------------ a. Reference parameter b. Value parameter c. Variable parameter d. Actual parameter.
Ans: a. Reference
15
a. 9 , 13 b. 1 , 2 c. 9 , 2 d. 5 , 2
Ans: c.
Q(69) Write the definition of a void function that takes as input two parameters of type int , say sum and testScore . The function updates the value of sum by adding the value of testScore . The new value of sum is reflected in the calling environment a. void Sum_fun( int &sum , int testScore )
{ sum += testScore ; } b. void Sum_fun( int sum , int &testScore )
{ sum += testScore ; } c. int Sum_fun( int sum , int testScore )
{ sum = sum +testScore ; return sum; } d. void Sum_fun( int sum , int testScore )
{ return ( sum +testScore ) ; }
Ans: a.
Q(70) void summer( int & a, int b)
{ int c = b + 2; a = 2 * c + 1; b = a + 4;
}
When a= 1 , b= 2 and invoke function summer(a, b); the value of a and b will be:
16
Q(71) Consider the following function definition: void defaultParam( int u, int v = 5, double z = 3.2)
{ int a; u = u + static_cast < int >( v + z); a = u + v; cout << "a = " << a << endl;
}
What is the output of the following function call defaultParam(1, 2); ? a. a=14 b. a= 8 c. a= 7 d. a=12
Ans: b.
Q(72) Write the definition of the function initialize that initializes x and y to zero and z to the blank character. a.void initialize( int x , int y , char z )
{ x=0; y=0; z= ” “ ; } b.void initialize( int x=0 , int y=0 , char z= ”” )
{ // comment } c.void initialize( int x , int y , char z )
{ x=zero; y=zero; z= “ blank ” ; } d.void initialize( int x , int y , char z )
{ x=0; y=0; z= ‘ ‘ ; }
Ans: d
Q(73) Write the definition of the function printCheck that prints the hours worked, rate per hour, and the salary. a.void Print ( double Hours , double rate , double salary )
{ cout<< Hours , rate , salary ; } b.void Print ( double Hours , double rate , double salary )
{ cout<< ( Hours , rate , salary ) ; } c.void Print ( double Hours , double rate , double salary )
{ cout<< Hours << rate << salary << endl ; } d.void Print ( double Hours , double rate , double salary )
{ cout << the hours worked = Hours
<< rate per hour = rate << and the salary= salary ; }
Ans: C
17
Q(74) Write the definition of the function NextChar that sets the value of z to the next character stored in z . a.
void NextChar( char & z)
{ int code= static_cast<int> z + 1 ;
z = static_cast<char> code ;
} b.void NextChar( char z)
{ int code= static_cast<int> z + 1 ;
z= static_cast<char> code ;
} c.void NextChar( char & z)
{ z= static_cast<char> (z+1) ; } d.void NextChar( char z)
{ z= static_cast<char> (z+1) ; }
Ans: a
Q(75) Write a function that takes as a parameter an integer (as a long value) and returns the number is odd, even, and zero. a.void Type( long x)
{ if(x%2) cout<< ” x is even ” ;
else if ( ! x%2) cout<< ” x is Odd ” ;
else cout<< ” x is Zero ” ;
} b.void Type( long x)
{ if(x==0) cout<< ” x is Zero ” ;
else if ( x%2) cout<< ” x is Odd ” ;
else cout<< ” x is even ” ;} c.void Type( long x)
{ if(x=0) cout<< ” x is Zero ” ;
else if ( x%2==0) cout<< ” x is Odd ” ;
else cout<< ” x is even ” ; } d.void Type( long x)
{ if(x <> 0) cout<< ” x is Zero ” ;
else if ( x%2) cout<< ” x is Odd ” ;
else cout<< ” x is even ” ;}
Ans: b
18
Q(75) The member variables of a class must be of the same type . a.
True b.
False.
Ans: b
Q(76) The member functions of a class must be public .
a.
True b.
False.
Ans: b
Q(77) A class can have more than one constructor. a.
True b.
False.
Ans: a
Q(78) A class can have more than one destructor. a.
True b.
False.
Ans: b
Q(79) Both constructors and destructors can have parameters. a.
True b.
False.
Ans: b
Q(80) Write the definition of the default constructor of the class xClass so that the private member variables int u; double w; are initialized to 0 . a.
xClass::xClass() { u =0 ; w=0 ; } b.
void xClass::xClass() { u =0 ; w=0 ;} c.
xClass::xClass( u =0 , w=0 ) { } d.
void xClass() { u =0 ; w=0 ; }
Ans: a
19
Q(81) The default data type in class is : a.
Double b.
Private c.
Protected d.
Auto private
Ans: b.
Q(82) Write a C++ statement that declares student to be a personType object, and initialize its first name to " Buddy " and last name to " Arora ". a. int main() { personType student(" Buddy " ," Arora " ); } b. int main() { student personType (" Buddy " ," Arora " ); } c. int main() { student student1 ( Buddy , Arora ); } d. int main() { personType personType (" Buddy " ," Arora " ); }
Ans: a.
Q(83) The purpose a destructor is a. Destroy the object and class b.
Destroy the object only c.
Deallocate memory of object and class d. Deallocate memory of object
Ans: d.
Q(84) A class member is accessed using the class variable name, by using a.
dot operator ( .
), followed by the member name.
b.
Resolution ( :: ) followed by the member name c. dot operator ( .
), followed by the class name.
d.
dot operator ( .
), followed by the object name.
Ans: a.
Q(85) A function can return a value of type a) Object. b) Class c) Function d) All
Ans: b.
20
Q(86) Constructors automatically execute when a) A class object is declared in main function. b) A class is written. c) Constructor function is called. d) All
Ans: a.
Q(87) Consider the definition of the following class : class testClass
{ private : int x; int y; public : int sum(); void print() const ; testClass(); testClass( int a, int b);
};
The constructor function is : a) testClass :: testClass( int a, int b) { a=x ; b=y; } b) testClass :: testClass( ) { a=x ; b=y; } c) testClass( int a, int b) { x=a ; y=b; } d) testClass :: testClass( int a, int b) : x(a) , y(b) { }
Ans: d.
21