Programming In C++ Solved MCQs


 Programming In C++ MCQs

This section contains more frequently asked Programming In C++ MCQs in the various University Level and Competitive Examinations.
 

Practice it now to sharpen your concepts and knowledge

 

1.The address of a variable temp of type float is
  • *temp
  • &temp
  • float& temp
  • float temp&
B. &temp
2.What is the output of the following code char symbol[3]={‘a’,‘b’,‘c’}; for (int index=0; index<3; index++) cout << symbol [index];
  • a b c
  • “abc”
  • abc
  • ‘abc’
C. abc
3.The process of building new classes from existing one is called ______.
  • Polymorphism
  • Structure
  • Inheritance
  • Cascading
C. Inheritance
4.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
  • protected and public data only in C and B.
  • protected and public data only in C.
  • private data in A and B.
  • protected data in A and B.
D. protected data in A and B.
5.If the variable count exceeds 100, a single statement that prints “Too many” is
  • if (count<100) cout << “Too many”;
  • if (count>100) cout >> “Too many”;
  • if (count>100) cout << “Too many”;
  • None of these
C. if (count>100) cout << “Too many”;
6.Usually a pure virtual function
  • has complete function body.
  • will never be called.
  • will be called only to delete an object.
  • is defined only in derived class.
D. is defined only in derived class.
7.To perform stream I/O with disk files in C++, you should
  • open and close files as in procedural languages
  • use classes derived from ios
  • use C language library functions to read and write data.
  • include the IOSTREAM.H header file.
B. use classes derived from ios
8.Overloading the function operator
  • requires a class with an overloaded operator.
  • requires a class with an overloaded [ ] operator.
  • allows you to create objects that act syntactically like functions
  • usually make use of a constructor that takes arguments.
A. requires a class with an overloaded operator.
9.If x = y,5 = 2 then x ∧ y equals________. (where ∧ is a bitwise XOR operator)
  • 00000111 
  • 10000010
  • 10100000 
  • 11001000
A. 00000111 

 

1.If an array is declared as int a[4] = {3, 0, 1, 2}, then values assigned to a[0] & a[4] will be ________
  • 3, 2
  • 0, 2
  • 3, 0
  • 0, 4
C. 3, 0
2.Mechanism of deriving a class from another derived class is known as____
  • Polymorphism
  • Single Inheritance
  • Multilevel Inheritance
  • Message Passing
C. Multilevel Inheritance
3.RunTime Polymorphism is achieved by ______
  • friend function
  • virtual function
  • operator overloading
  • function overloading
B. virtual function
4.A function call mechanism that passes arguments to a function by passing a copy of the values of the arguments is __________
  • call by name
  • call by value
  • call by reference
  • call by value result
B. call by value
5.In C++, dynamic memory allocation is accomplished with the operator ____
  • new
  • this
  • malloc( )
  • delete
A. new
6.If we create a file by ‘ifstream’, then the default mode of the file is _________
  • ios :: out
  • ios :: in
  • ios :: app
  • ios :: binary
B. ios :: in
7.A variable defined within a block is visible
  • from the point of definition onward in the program.
  • from the point of definition onward in the function.
  • from the point of definition onward in the block.
  • throughout the function.
C. from the point of definition onward in the block.
8.The break statement causes an exit
  • from the innermost loop only.
  • only from the innermost switch.
  • from all loops & switches.
  • from the innermost loop or switch.
D. from the innermost loop or switch.
9.Which of the following cannot be legitimately passed to a function
  • A constant.
  • A variable.
  • A structure.
  • A header file.
D. A header file.
10.A property which is not true for classes is that they
  • are removed from memory when not in use.
  • permit data to be hidden from other classes
  • bring together all aspects of an entity in one place.
  • Can closely model objects in the real world.
C. bring together all aspects of an entity in one place.

 

1.You can read input that consists of multiple lines of text using
  • the normal cout << combination.
  • the cin.get( ) function with one argument.
  • the cin.get( ) function with two arguments.
  • the cin.get( ) function with three arguments.
C. the cin.get( ) function with two arguments.
2.The keyword friend does not appear in
  • the class allowing access to another class.
  • the class desiring access to another class.
  • the private section of a class.
  • the public section of a class.
C. the private section of a class.
3.The process of building new classes from existing one is called
  • Structure
  • Inheritance
  • Polymorphism
  • Template
B. Inheritance
4.If you wanted to sort many large objects or structures, it would be most efficient to
  • place them in an array & sort the array.
  • place pointers to them in an array & sort the array.
  • place them in a linked list and sort the linked list.
  • place references to them in an array and sort the array
C. place them in a linked list and sort the linked list.
5.Which statement gets affected when i++ is changed to ++i?
  • i = 20; i++;
  • for (i = 0; i<20; i++) { } 
  • a = i++; 
  • while (i++ = 20) cout i;
A. i = 20; i++;
6.A friend function to a class, C cannot access
  • private data members and member functions.
  • public data members and member functions.
  • protected data members and member functions.
  • the data members of the derived class of C.
D. the data members of the derived class of C.
7.The operator that cannot be overloaded is
  • ++
  • ::
  • ( )
  • ~
B. ::
8.A struct is the same as a class except that
  • there are no member functions.
  • all members are public.
  • cannot be used in inheritance hierarchy.
  • it does have a this pointer.
C. cannot be used in inheritance hierarchy.
9.Pure virtual functions
  • have to be redefined in the inherited class.
  • cannot have public access specification.
  • are mandatory for a virtual class.
  • None of the above.
A. have to be redefined in the inherited class.
10.Additional information sent when an exception is thrown may be placed in
  • the throw keyword.
  • the function that caused the error.
  • the catch block.
  • an object of the exception class.
C. the catch block.

 

1.A virtual class is the same as
  • an abstract class
  • a class with a virtual function
  • a base class
  • none of the above.
D. none of the above.
2.Identify the operator that is NOT used with pointers
  • ->
  • &
  • *
  • >>
D. >>
3.Consider the following statements char *ptr; ptr = “hello”; cout << *ptr; What will be printed?
  • first letter
  • entire string
  • it is a syntax error
  • last letter
A. first letter
4.In which case is it mandatory to provide a destructor in a class?
  • Almost in every class
  • Class for which two or more than two objects will be created
  • Class for which copy constructor is defined
  • Class whose objects will be created dynamically
D. Class whose objects will be created dynamically
5.The members of a class, by default, are
  • public
  • protected
  • private
  • mandatory to specify
C. private
6.Given a class named Book, which of the following is not a valid constructor?
  • Book ( ) { }
  • Book ( Book b) { }
  • Book ( Book &b) { }
  • Book (char* author, char* title) { }
B. Book ( Book b) { }
7.Which of the statements is true in a protected derivation of a derived class from a base class?
  • Private members of the base class become protected members of the derived class
  • Protected members of the base class become public members of the derived class
  • Public members of the base class become protected members of the derived class
  • Protected derivation does not affect private and protected members of the 
C. Public members of the base class become protected members of the derived class
8.Which of the following statements is NOT valid about operator overloading?
  • Only existing operators can be overloaded.
  • Overloaded operator must have at least one operand of its class type.
  • The overloaded operators follow the syntax rules of the original operator.
  • none of the above.
D. none of the above.
9.Exception handling is targeted at
  • Run-time error
  • Compile time error
  • Logical error
  • All of the above.
A. Run-time error
10.A pointer to the base class can hold address of
  • only base class object
  • only derived class object
  • base class object as well as derived class object
  • None of the above
C. base class object as well as derived class object

 

1.Use of virtual functions implies
  • overloading
  • overriding
  • static binding
  • dynamic binding
D. dynamic binding
2.this pointer
  • implicitly points to an object
  • can be explicitly used in a class
  • can be used to return an object
  • All of the above
D. All of the above
3.Within a switch statement
  • Continue can be used but Break cannot be used
  • Continue cannot be used but Break can be used
  • Both Continue and Break can be used
  • Neither Continue nor Break can be used
B. Continue cannot be used but Break can be used
4.Data members which are static
  • cannot be assigned a value
  • can only be used in static functions
  • cannot be defined in a Union
  • can be accessed outside the class
B. can only be used in static functions
5.Which of the following is false for cin?
  • It represents standard input.
  • It is an object of istream class.
  • It is a class of which stream is an object.
  • Using cin the data can be read from user’s terminal
C. It is a class of which stream is an object.
6.It is possible to declare as a friend
  • a member function
  • a global function
  • a class
  • all of the above
D. all of the above
7.In multiple inheritance
  • the base classes must have only default constructors
  • cannot have virtual functions
  • can include virtual classes
  • None of the above.
C. can include virtual classes
8.Declaration of a pointer reserves memory space
  • for the object.
  • for the pointer.
  • both for the object and the pointer.
  • none of these.
B. for the pointer.
9.for (; ?
  • means the test which is done using some expression is always true
  • is not valid
  • will loop forever
  • should be written as for( )
C. will loop forever
10.The operator << when overloaded in a class
  • must be a member function
  • must be a non member function
  • can be both (A) & (B) above
  • cannot be overloaded
C. can be both (A) & (B) above

 

1.Function templates can accept
  • any type of parameters
  • only one parameter
  • only parameters of the basic type
  • only parameters of the derived type
C. only parameters of the basic type
2.How many constructors can a class have?
  • 0
  • 1
  • 2
  • any number
D. any number
3.The new operator
  • returns a pointer to the variable
  • creates a variable called new
  • obtains memory for a new variable
  • tells how much memory is available
C. obtains memory for a new variable
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?
  •  22
  • 37
  • 7
  • Error. Cannot be executed
B. 37
5.An exception is caused by
  • a hardware problem
  • a problem in the operating system
  • a syntax error
  • a run-time error
D. a run-time error
6.A template class
  • is designed to be stored in different containers
  • works with different data types
  • generates objects which must be identical
  • generates classes with different numbers of member functions
B. works with different data types
7.Which of the following is the valid class declaration header for the derived class d with base classes b1 and b2?
  • class d : public b1, public b2
  • class d : class b1, class b2
  • class d : public b1, b2
  • class d : b1, b2
A. class d : public b1, public b2
8.A library function exit() causes an exit from
  • the loop in which it occurs
  • the block in which it occurs
  •  the function in which it occurs
  • the program in which it occurs
D. the program in which it occurs
9.RunTime polymorphism is achieved by ___________
  • friend function
  • virtual function
  • operator overloading
  • function overloading
B. virtual function
10.Declaration of a pointer reserves memory space
  • for the object.
  • for the pointer.
  • both for the object and the pointer.
  • none of these.
B. for the pointer.

 

1.An array element is accessed using
  • a FIFO approach
  • an index number
  • the operator
  • a member name
B. an index number
2.A pure virtual function is a virtual function that
  • has no body
  • returns nothing
  • is used in base class
  • both (A) and (C)
D. both (A) and (C)
3.A static function
  • should be called when an object is destroyed.
  • is closely connected with and individual object of a class.
  • can be called using the class name and function name.
  • is used when a dummy object must be created.
C. can be called using the class name and function name.
4.We can output text to an object of class ostream using the insertion operator << because
  • the ostream class is a stream
  • the insertion operator works with all classes.
  • we are actually outputting to cout.
  • the insertion operator is overloaded in ostream
D. the insertion operator is overloaded in ostream
5.The statement f1.write((char*)&obj1, sizeof(obj1));
  • writes the member function of obj1 to f1.
  • Writes the data in obj1 to f1.
  • Writes the member function and the data of obj1 to f1
  • Writes the address of obj1 to f1.
B. Writes the data in obj1 to f1.
6.To convert from a user defined class to a basic type, you would most likely use.
  • A built-in conversion function
  • A one-argument constructor
  • A conversion function that’s a member of the class.
  • An overloaded ‘=‘ operator.
C. A conversion function that’s a member of the class.
7.Which of the following is not the characteristic of constructor.
  • They should be declared in the public section.
  • They do not have return type.
  • They can not be inherited.
  • They can be virtual.
D. They can be virtual.
8.Name the header file to be included for the use of built in function isalnum()
  • string.h
  • process.h
  • ctype.h
  • dos.h
C. ctype.h

 

1.A class defined within another class is:
  • Nested class
  • Inheritance
  • Containership
  • Encapsulation
A. Nested class
2.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++;
  • x=25, m=10, n=15
  • x=26, m=11, n=16
  • x=27, m=11, n=16
  • x=27, m=10, n=15
B. x=26, m=11, n=16
3.Which of the following will produce a value 10 if x = 9.7?
  • floor(x)
  • abs(x)
  • log(x)
  • ceil(x)
D. ceil(x)
4.The major goal of inheritance in c++ is:
  • To facilitate the conversion of data types.
  • To help modular programming.
  • To extend the capabilities of a class.
  • To hide the details of base class.
C. To extend the capabilities of a class.
5.Consider the following class definitions: class a { }; class b: protected a { }; What happens when we try to compile this class?
  • Will not compile because class body of a is not defined.
  • Will not compile because class body of b is not defined
  • Will not compile because class a is not public inherited
  • Will compile successfully.
D. Will compile successfully.
6.Which of the following expressions is illegal?
  • (10 6).
  • (false && true)
  • bool (x) = (bool)10;
  • float y = 12.67;
C. bool (x) = (bool)10;
7.The actual source code for implementing a template function is created when
  • The declaration of function appears.
  • The function is invoked.
  • The definition of the function appears.
  • None of the above.
B. The function is invoked.
8.An exception is caused by
  • a runtime error.
  • a syntax error.
  • a problem in the operating system.
  • a hardware problem.
A. a runtime error.
9.Which of the following statements are true in c++?
  • Classes can not have data as public members.
  • Structures can not have functions as members.
  • Class members are public by default.
  • None of these.
B. Structures can not have functions as members.
10.What would be the output of the following program? int main() { int x,y=10,z=10; x = (y = =z); cout<<
  • 1
  • 0
  • 10
  • Error
A. 1

 


1.What is the error in the following code? class t { virtual void print(); }
  • No error.
  • Function print() should be declared as static.
  • Function print() should be defined.
  • Class t should contain data members.
A. No error.
2.What will be the output of following program? #include void main() { float x; x=(float)9/2; cout x; }
  •  4.5
  •  4.0
  • 4
  • 5
A.  4.5
3.A white space is :
  • blank space
  • new line
  • tab
  • all of the above
D. all of the above
4.The following can be declared as friend in a class
  • an object
  • a class
  • a public data member
  • a private data member
B. a class
5.What would be the output of the following? #include void main() { char *ptr=“abcd” char ch; ch = ++*ptr++; cout ch; }
  • a
  • b
  • c
  • d
B. b
6.A copy constructor takes
  • no argument
  • one argument
  •  two arguments
  • arbitrary no. of arguments
B. one argument
7.Overloading a postfix increment operator by means of a member function takes
  • no argument
  • one argument
  • two arguments
  • three arguments
A. no argument
8.Which of the following ways are legal to access a class data member using this pointer?
  •  this.x
  • *this.x
  • *(this.x)
  • (*this).x
D. (*this).x
9.If we store the address of a derived class object into a variable whose type is a pointer to the base class, then the object, when accessed using this pointer.
  • continues to act like a derived class object.
  • Continues to act like a derived class object if virtual functions are called.
  • Acts like a base class object.
  • Acts like a base class, if virtual functions are called.
B. Continues to act like a derived class object if virtual functions are called.
10.Which of the following declarations are illegal?
  •  void *ptr;
  • char *str = “hello”;
  • char str = “hello”;
  • const *int p1;
C. char str = “hello”;

 

1.What will be the result of the expression 13 & 25?
  • 38
  • 25
  • 9
  • 12
C. 9
2.Which of the following operator can be overloaded through friend function?
  • ->
  • =
  • ( )
  • *
D. *
3.To access the public function fbase() in the base class, a statement in a derived class function fder() uses the statement.fbase();
  • fbase();
  • fder();
  • base::fbase();
  • der::fder();
A. fbase();
4.If a base class destructor is not virtual, then
  • It can not have a function body.
  • It can not be called.
  • It can not be called when accessed from pointer.
  • Destructor in derived class can not be called when accessed through a pointer to the base class.
D. Destructor in derived class can not be called when accessed through a pointer to the base class.
5.Maximum number of template arguments in a function template is
  • one
  • two
  • three
  • many
D. many
6.In access control in a protected derivation, visibility modes will change as follows:
  • private, public and protected become protected
  • only public becomes protected.
  •  public and protected become protected.
  • only private becomes protected.
C.  public and protected become protected.
7.Which of the following statement is valid?
  • We can create new C++ operators.
  • We can change the precedence of the C++ operators.
  • We can change the associativity of the C++ operators.
  • We can not change operator templates.
D. We can not change operator templates.
8.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; }
  • 1
  •  1.0
  • Error message
  • 2.5
C. Error message
9.Which can be passed as an argument to a function?
  • constant
  • expression
  • another function
  • all of the above.
A. constant
10.Member functions, when defined within the class specification:
  • are always inline.
  • are not inline.
  • are inline by default, unless they are too big or too complicated.
  • are not inline by default.
A. are always inline.

 


Post a Comment

Previous Post Next Post