C++ OBJECTIVE QUESTIONS USEFUL FOR CODING COMPETITIONS
BASICS1) The process of building new classes from existing one is called ______. (A) Polymorphism (B) Structure (C) Inheritance (D) Cascading Ans: C 2) If a class C is derived from class B, which is derived from class A, all through public inheritance, then a class C member function can access (A) protected and public data only in C and B. (B) protected and public data only in C. (C) private data in A and B. (D) protected data in A and B. Ans: D 3) In C++, the range of signed integer type variable is ________(A) 0 to 216 (B) 2 to 2 1 15 15 − −(C) 2 to 2 1 7 7 − − (D) 8 0 to 2 Ans: B 4) To perform stream I/O with disk files in C++, you should (A) open and close files as in procedural languages. (B) use classes derived from ios. (C) use C language library functions to read and write data. (D) include the IOSTREAM.H header file. Ans: B 5) RunTime Polymorphism is achieved by ______ (A) friend function (B) virtual function (C) operator overloading (D) function overloading Ans: B 6) In C++, dynamic memory allocation is accomplished with the operator ____ (A) new (B) this (C) malloc( ) (D) delete Ans: A 7) If we create a file by ‘ifstream’, then the default mode of the file is (A) ios :: out (B) ios :: in (C) ios :: app (D) ios :: binary Ans: B 8) A variable defined within a block is visible (A) from the point of definition onward in the program. (B) from the point of definition onward in the function. (C) from the point of definition onward in the block. (D) throughout the function. Ans: C 9) The break statement causes an exit from (A) from the innermost loop only. (B) only from the innermost switch. (C) from all loops & switches. (D) from the innermost loop or switch. Ans: D 10) A property which is not true for classes is that they (A) are removed from memory when not in use. (B) permit data to be hidden from other classes. (C) bring together all aspects of an entity in one place. (D) Can closely model objects in the real world. Ans: C 11) You can read input that consists of multiple lines of text using (A) the normal cout << combination. (B) the cin.get( ) function with one argument. (C) the cin.get( ) function with two arguments. (D) the cin.get( ) function with three arguments. Ans: C 12) The keyword friend does not appear in (A) the class allowing access to another class. (B) the class desiring access to another class. (C) the private section of a class. (D) the public section of a class. Ans: C 13) The operator that cannot be overloaded is (A) ++ (B) :: (C) ( ) (D) ~ Ans: B 14) A struct is the same as a class except that (A) there are no member functions. (B) all members are public. (C) cannot be used in inheritance hierarchy. (D) it does have a this pointer. Ans: C 15) Pure virtual functions (A) have to be redefined in the inherited class. (B) cannot have public access specification. (C) are mandatory for a virtual class. (D) None of the above. Ans: A 16) Additional information sent when an exception is thrown may be placed in (A) the throw keyword. (B) the function that caused the error. (C) the catch block. (D) an object of the exception class. Ans: C 17) Use of virtual functions implies (A) overloading. (B) overriding. (C) static binding. (D) dynamic binding. Ans: D 18) this pointer (A) implicitly points to an object. (B) can be explicitly used in a class. (C) can be used to return an object. (D) All of the above. Ans: D 19) Within a switch statement (A) Continue can be used but Break cannot be used (B) Continue cannot be used but Break can be used (C) Both Continue and Break can be used (D) Neither Continue nor Break can be used Ans:B 20) Data members which are static (A) cannot be assigned a value (B) can only be used in static functions (C) cannot be defined in a Union (D) can be accessed outside the class Ans:B 21) The members of a class, by default, are (A) public (B) protected (C) private (D) mandatory to specify Ans:C 22) Given a class named Book, which of the following is not a valid constructor? (A) Book ( ) { } (B) Book ( Book b) { } (C) Book ( Book &b) { } (D) Book (char* author, char* title) { } Ans:B 23) Exception handling is targeted at (A) Run-time error (B) Compile time error (C) Logical error (D) All of the above. Ans:A 24) A pointer to the base class can hold address of (A) only base class object (B) only derived class object (C) base class object as well as derived class object (D) None of the above Ans:C 25) How many constructors can a class have? (A) 0 (B) 1 (C) 2 (D) any number Ans:D |
CODING1) If the variable count exceeds 100, a single statement that prints “Too many” is (A) if (count<100) cout << “Too many”; (B) if (count>100) cout >> “Too many”; (C) if (count>100) cout << “Too many”; (D) None of these. Ans:C 2) If x = 5, y = 2 then x Ù y equals________. (where Ù is a bitwise XOR operator) (A) 00000111 (B) 10000010 (C) 10100000 (D) 11001000 Ans: A 3) If an array is declared asint a[4] = {3, 0, 1, 2}, then values assigned to a[0] & a[4] will be ________ (A) 3, 2 (B) 0, 2 (C) 3, 0 (D) 0, 4 Ans: C 4) Consider the following statements: int x = 22, y=15; x = (x>y) ? (x+y) : (x-y); What will be the value of x after executing these statements? (A) 22 (B) 37 (C) 7 (D) Error. Cannot be executed Ans:B 5) What is the output of given code fragment? int f=1, i=2; while(++i<5) f*=i; cout<<f; (A) 12 (B) 24 (C) 6 (D) 3 Ans:A 6) What will be the values of x, m and n after the execution of the following statements? int x, m, n; m = 10; n = 15; x = ++m + n++; (A) x=25, m=10, n=15 (B) x=26, m=11, n=16 (C) x=27, m=11, n=16 (D) x=27, m=10, n=15 Ans:B 7) Which of the following will produce a value 10 if x = 9.7? (A) floor(x) (B) abs(x) (C) log(x) (D) ceil(x) Ans:D 8) Which of the following expressions is illegal? (A) (10 6). (B) (false && true) (C) bool (x) = (bool)10; (D) float y = 12.67; Ans:C 9) What would be the output of the following program? int main() { int x,y=10,z=10; x = (y = =z); cout<<x; return 0; } (A) 1 (B) 0 (C) 10 (D) Error Ans:A 10) What is the error in the following code? class t { virtual void print(); } (A) No error. (B) Function print() should be declared as static. (C) Function print() should be defined. (D) Class t should contain data members. Ans:A 11) What will be the output of following program? #include<iostream.h> void main() { float x; x=(float)9/2; cout<<x; } (A) 4.5 (B) 4.0 (C) 4 (D) 5 Ans:A 12) What would be the output of the following? #include<iostream.h> void main() { char *ptr=“abcd” char ch;ch = ++*ptr++; } (A) a (B) b (C) c (D) d Ans:B 13) What will be the output of the following program? #include<iostream.h> void main() { float x=5,y=2; int result; result=x % y; cout<<result; } (A) 1 (B) 1.0 (C) Error message (D) 2.5 Ans:C |