C/C /CPP Programming MCQ Set 1 Sample Test,Sample questions

Question:
 How can we restrict dynamic allocation of objects of a class using new?

1. By overloading new operator

2.By making an empty private new operator.

3.By making an empty private new and new[] operators

4.By overloading new operator and new[] operators


Question:
 Is it fine to call delete twice for a pointer?
#include<iostream>
using namespace std;
int main()
{
int *ptr = new int;
delete ptr;
delete ptr;
return 0;
}

1.Yes

2.No

3.none

4.all


Question:
 Output of following program?
#include <iostream>
using namespace std;
int fun(int=0, int = 0);
int main()
{
cout << fun(5);
return 0;
}
int fun(int x, int y)
{
return (x+y);
}

1.Compiler Error

2.5

3.10

4.none of the above


Question:
 Predict the output of following C++ program.
#include<iostream>
using namespace std;
class Empty {};
int main() {
cout << sizeof(Empty);
return 0;
}

1.A non-zero value

2.0

3.Compiler Error

4.Runtime Error


Question:
 What happens when delete is used for a NULL pointer?
int *ptr = NULL;
delete ptr;

1.Compiler Error

2.Run-time Crash

3. No Error

4.None


Question:
 What is the use of this pointer?

1.When local variable’s name is same as member’s name, we can access member using this pointer.

2.To return reference to the calling object

3.Can be used for chained function calls on an object

4.All of the above


Question:
 When a copy constructor may be called?

1. A. When an object of the class is returned by value.

2.When an object of the class is passed (to a function) by value as an argument.

3.When an object is constructed based on another object of the same class

4.All of the above


Question:
 When a method in a subclass has the same name and type signatures as a method in thesuperclass, then the method in the subclass _____ the method in the superclass.

1.Overloads

2.Friendships

3.Inherits

4.Overrides


Question:
 When the inheritance is private, the private methods in base class are __________ in thederived class (in C++).

1.inaccessible

2.accessible

3.protected

4.public


Question:
 Which of the following is not a member of class?

1. Static function

2.Friend function

3.Const function

4.Virtual function


Question:
 Which of the following is true about constructors?
1) They cannot be virtual.
2) They cannot be private.
3) They are automatically called by new operator.

1.All 1, 2, and 3

2.Only 1 and 3

3.Only 1 and 2

4.Only 2 and 3


Question:
 Which of the following is true about pure virtual functions?
1) Their implementation is not provided in a class where they are declared.
2) If a class has a pure virtual function, then the class becomes abstract class and an
instance of this class cannot be created.

1. Both 1 and 2

2.Only 1

3.Only 2

4. Neither 1 nor 2


Question:
 Which of the following is true?

1.All objects of a class share all data members of class

2.Objects of a class do not share non-static members. Every object has its own copy.

3.Objects of a class do not share codes of non-static methods, they have their own copy

4.None of the above


Question:
 Which of the following is true?

1.Static methods cannot be overloaded.

2.Static data members can only be accessed by static methods.

3. Non-static data members can be accessed by static methods.

4.Static methods can only access static members (data and methods)


Question:
 Which of the following operators should be preferred to overload as a global function rather than a member method?

1.Postfix ++

2.Comparison Operator

3.Insertion Operator <<

4.Prefix++


Question:
 Which of the following, in C++, is inherited in a derived class from base class?

1. Constructor

2.Destructor

3.Data members

4.Virtual methods


Question:
 Which of the followings is/are automatically added to every class, if we do not write ourown?

1.Copy Constructor

2.Assignment Operator

3.A constructor without any parameter

4.All of the above


Question:
 Which one of the following is correct, when a class grants friend status to another class?

1.The member functions of the class generating friendship can access the members of the friend class.

2.All member functions of the class granted friendship have unrestricted access to the members of the class granting the friendship.

3. Class friendship is reciprocal to each other.

4. Class friendship is reciprocal to each other.


Question:
#include<iostream>
using namespace std;
class Point {
public:
Point() { cout << "Constructor called"; }
};
int main()
{
Point t1, *t2;
return 0;
}

1.Compiler Error

2.Constructor called Constructor called

3.Constructor called

4.None of the above


Question:
A member function can always access the data in __________, (in C++).

1. the class of which it is member

2. the object of which it is a member

3. the public part of its class

4. the private part of its class


Question:
class Test {
int x;
};
int main() {
Test t;
cout << t.x;
return 0;
}

1.0

2.Garbage Value

3.Compiler Error

4.None of These


Question:
How C++ compiler does differ between overloaded postfix and prefix operators?

1.C++ doesn’t allow both operators to be overloaded in a class

2. A postfix ++ has a dummy parameter

3.A prefix ++ has a dummy parameter

4.By making prefix ++ as a global function and postfix as a member function.


Question:
How to create a dynamic array of pointers (to integers) of size 10 using new in C++?Hint: We can create a non-dynamic array using int *arr[10]

1.int *arr = new int *[10];

2.int **arr = new int *[10];

3.int *arr = new int [10];

4.Not Possible


Question:
If a function is friend of a class, which one of the following is wrong?

1.A function can only be declared a friend by a class itself.

2.Friend functions are not members of a class, they are associated with it.

3.Friend functions are members of a class.

4.It can have access to all members of the class, even private ones.


Question:
Implicit return type of a class constructor is:

1. not of class type itself

2.class type itself

3.a destructor of class type

4.a destructor not of class type


Question:
In C++, const qualifier can be applied to
1) Member functions of a class
2) Function arguments
3) To a class data member which is declared as static
4) Reference variables

1.Only 1, 2 and 3

2.Only 1, 2 and 4

3.All

4.Only 1, 3 and 4


Question:
It is possible to define a class within a class termed as nested class. There are _____ types of nested classes.

1.2

2.3

3.4

4.5


Question:
Output of following C++ program?
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int& ref = x;
ref = 20;
cout << "x = " << x << endl ;
x = 30;
cout << "ref = " << ref << endl;
return 0;
}

1.x = 20; ref = 30

2.x = 20; ref = 20

3.x = 10; ref = 30

4.x = 30; ref = 30


Question:
Output of following program?
#include<iostream>
using namespace std;
class Point {
Point() { cout << "Constructor called"; }
}; int main()
{
Point t1;
return 0;
}

1.Compiler Error

2.Runtime Error

3.Constructor called

4.None of the above


Question:
Output of the program?
#include<iostream>
using namespace std;
int fun(int x = 0, int y = 0, int z)
{ return (x + y + z); }
int main()
{
cout << fun(10);
return 0;
}

1. 10

2.0

3.20

4.Compiler Error


Question:
Predict the output?
#include <iostream>
using namespace std;
class Test
{
int x;
Test()
{
x = 5;
} };
int main()
{
Test *t = new Test;
cout << t->x;
}

1.Compiler Error

2.5

3.Garbage Value

4.None of these


Question:
What is the difference between struct and class in C++?

1.All members of a structure are public and structures don’t have constructors and destructors

2.Members of a class are private by default and members of struct are public by default. When deriving a struct from a class/struct, default access-specifier for a base class/struct is public and when d

3.All members of a structure are public and structures don’t have virtual functions

4.All of the above


Question:
What is the size of wchar_t in C++?

1. 2

2.4

3.2 or 4

4.Based on the number of bits in the system


Question:
When one object reference variable is assigned to another object reference variable then

1.a copy of the object is created.

2.a copy of the reference is created.

3.a copy of the reference is not created.

4.it is illegal to assign one object reference variable to another object reference variable.


Question:
Which of the following cannot be passed to a function in C++?

1. Constant

2.Structure

3.Array

4. Header file


Question:
Which of the following functions must use reference?

1.Assignment operator function

2.Copy Constructor

3.Destructor

4.Parameterized constructor


Question:
Which of the following in Object Oriented Programming is supported by Function overloading and default arguments features of C++?

1.Inheritance

2.Polymorphism

3.Encapsulation

4.None of the above


Question:
Which of the following is a correct statement?

1. Composition is a strong type of association between two classes with full ownership

2.Composition is a strong type of association between two classes with partial ownership.

3.Composition is a weak type of association between two classes with partial ownership.

4. Composition is a weak type of association between two classes with strong ownership.


Question:
Which of the following is FALSE about references in C++?

1.References cannot be NULL

2.A reference must be initialized when declared

3. Once a reference is created, it cannot be later made to reference another object; it cannot be reset.

4.References cannot refer to constant value


Question:
Which of the following is not a correct statement?

1.Every class containing abstract method must be declared abstract.

2. Abstract class can directly be initiated with ‘new’ operator.

3.Abstract class can be initiated.

4. Abstract class does not contain any definition of implementation.


Question:
Which of the following is not correct (in C++)?

1. Class templates and function templates are instantiated in the same way
2. Class templates differ from function templates in the way they are initiated
3. Class template is initiated by defining an object using the template argument
4. Class templates are generally used for storage classes

1.(1)

2.(2), (4) C.

3. (2), (3), (4)

4. (4)


Question:
Which of the following is not correct for virtual function in C++?

1.Must be declared in public section of class.

2. Virtual function can be static.

3.Virtual function should be accessed using pointers.

4.Virtual function is defined in base class.


Question:
Which of the following is true about new when compared with malloc:
1) new is an operator, malloc is a function
2) new calls constructor, malloc doesn’t
3) new returns appropriate pointer, malloc returns void * and pointer needs to typecast to appropriate type.

1. 1 and 3

2.2 and 3

3.1 and 2

4.All 1, 2 and 3


Question:
Which of the following is true about this pointer?

1. It is passed as a hidden argument to all function calls

2.It is passed as a hidden argument to all non-static function calls

3.It is passed as a hidden argument to all static functions

4.None of the above


Question:
Which of the following is true about virtual functions in C++?

1.Virtual functions are functions that can be overridden in derived class with the same signature.

2. Virtual functions enable run-time polymorphism in a inheritance hierarchy.

3.If a function is ‘virtual’ in the base class, the most-derived class implementation of the function is called according to the actual type of the object referred to, regardless of the declared typ

4.All of the above


Question:
Which of the following operator functions cannot be global?

1.new

2.delete

3.Conversion Operator

4.All of the above


Question:
Which of the following operators are overloaded by default by the compiler in every user defined classes even if user has not written? 1) Comparison Operator (==) 2) Assignment Operator (=)

1.Both 1 and 2

2.Only 1

3.Only 2

4. None of the two


Question:
Which of the following operators cannot be overloaded?

1. (Member Access or Dot operator)

2.?: (Ternary or Conditional Operator)

3.:: (Scope Resolution Operator

4.All of the above


Question:
Which operator is having the highest precedence?

1.postfix

2.unary

3.shift

4.equality


More MCQS

  1. C++ Programming MCQS Set-1
  2. C++ Multiple Choice Questions Set-1
  3. C++ Multiple Choice Questions Set-2
  4. C++ Programming MCQS Set-2
  5. C++ Programming MCQS Set-3
  6. C++ Programming MCQS Set-4
  7. C++ (CPP) MCQ Question with Answer
  8. Advanced c++ multiple choice question(MCQS)
  9. OOPS Quiz Questions and Answers(MCQS)
  10. C Programming - MCQ Questions Set 1
  11. C Programming - MCQ Questions Set 2
  12. C Programming - MCQ Questions Set 3
  13. C Programming - MCQ Questions Set 4
  14. C Programming - MCQ Questions Set 5
  15. C++ programming language MCQ Questions Set 1
  16. C++ programming language MCQ Questions Set 2
  17. C++ programming language MCQ Questions Set 3
  18. C++ programming language MCQ Questions Set 4
  19. C++ programming language MCQ Questions Set 5
  20. C++ Programming -Constructors and Destructors
  21. C++ Programming -OOPS Concepts
  22. C++ Programming - References
  23. C++ Programming - Functions
  24. C MCQS:-The ABC of C
  25. C MCQS Interview Questions
  26. C++ Questions and Answers OOPs Basic Concepts
  27. C++ Questions and Answers Returning Objects
  28. C Programming MCQ Part 1
  29. C Programming MCQ
  30. Computer Science & Engineering C Multiple Choice Questions set 1
  31. Computer Science & Engineering C Multiple Choice Questions set 2
  32. C Multiple Choice Questions C Functions Set 1
  33. C Multiple Choice Questions C Functions Set 2
  34. C Multiple Choice Questions C Operators
  35. C Multiple Choice Questions & AnswersConditional Expressions
  36. C Multiple Choice Questions & Answers Data Types
  37. C Multiple Choice Questions & Answers File Access
  38. Computer Science & Engineering Cloud Computing MCQs Part 1
  39. CPP Programming MCQ Set 1
  40. CPP Programming MCQ Set 2
Search
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!