MCQ with answers for (OOP) | Object Oriented Programming | 22316 [Object & classes] MSBTE

Subject subject details
Program: Diploma in Computer engineering
Program Code CO
Scheme I
Semester 3
Course: Object Oriented Programming
Course Code 22316


Content of Chapter:-


2.1 Class & Object: Introduction, specifying a class ,access specifiers, defining member functions, creatingObjects, memory allocations for objects.
2.2 Static data members, static member function, friend Function.
2.3 Array of Objects, Object as function arguments.
2.4 Concepts of Constructors, Types of constructors.
2.5 Multiple Constructors in a Class, Constructors with default arguments.
2.6 Destructors.

 

1. Which of the following is not correct for virtual function in C++ ?.

A. Virtual function can be static.
B. Virtual function should be accessed using pointers
C. Virtual function is defined in base class
D. Must be declared in public section of class

Ans : A

Explanation: Virtual function is can’t be static in C++.

2. How can we make a class abstract?

A. By declaring it abstract using the static keyword
B. By declaring it abstract using the virtual keyword.
C. By making at least one member function as pure virtual function
D. By making all member functions constant

 

View Answer

Ans : C

Explanation: We can make a class abstract by making at least one member function as pure virtual function.

 

3. How many specifiers are present in access specifiers in class?

A. 2
B. 1
C. 4
D. 3

View Answer

Ans : D

Explanation: There are three types of access specifiers. They are public, protected and private.

4. Which of these following members are not accessed by using direct member access operator?

A. Public
B. Private
C. Protected
D. Both B & C

 

View Answer

Ans : D

Explanation: Because of the access is given to the private and protected, We can’t access them by using direct member access operator.



5. Which other keywords are also used to declare the class other than class?
A. Struct
B. Union
C. Object
D. Both struct & union

View Answer

Ans : D

Explanation: Struct and union take the same definition of class but differs in the access techniques.

6. Which of the following is true?

A. All objects of a class share all data members of class
B. Objects of a class do not share non-static members. Every object has its own copy
C. Objects of a class do not share codes of non-static methods, they have their own copy
D. None of these

 

View Answer

Ans : B

Explanation: very object maintains a copy of non-static data members. For example, let Student be a class with data members as name, year, batch. Every object of student will have its own name, year and batch. On a side note, static data members are shared among objects. All objects share codes of all methods

7. Which of the following can be overloaded?

A. Object
B. Operators
C. Both A & B
D. None of the above

 

View Answer

Ans : C

Explanation: Object and Operators can be overloaded.

8. Which is also called as abstract class?

A. Virtual function
B. Derived class
C. Pure virtual function
D. None of the mentioned

 

View Answer

Ans : C

Explanation: Classes that contain at least one pure virtual function are called as abstract base classes.

 

9. What will be the output of the following program?

#include <iostream>

 

using namespace std;

class LFC

{

    static int x;

    public:

    static void Set(int xx)

    {

        x = xx;

    }

    void Display()

    {

        cout<< x ;

    }

};

int LFC::x = 0;

int main()

{

    LFC::Set(33);

    LFC::Display();

    return 0;

}

A. The program will print the output 0.
B. The program will print the output 33.
C. The program will print the output Garbage.
D. The program will report compile time error.

 

View Answer

Ans : D

Explanation: The program will report compile time error: cannot call member function "void LFC::Display()" without object

 

10. What will be the output of the following program?

        

Note:Includes all required header files

class course

{

    int x, y;

    public:

    course(int xx)

    {

        x = ++xx;

    }

    void Display()

    {

        cout<< --x << " ";

    }

};

int main()

{

    course obj(20);

    obj.Display();

    int *p = (int*)&obj ;

    *p = 5;

    obj.Display();

    return 0;

}

A. 20 4
B. 21 4
C. 20 5
D. 21 5

 

View Answer

Ans : A

Explanation: 20 4 will be the output of the following program

 

11. What is the output of this program?

        

Note:Includes all required header files

using namespace std;

 

class Empty {};

 

int main()

{

  cout << sizeof(Empty);

  return 0;

}

A. A non-zero value.
B. 0
C. Compiler Error
D. Runtime Error

View Answer

Ans : A

Explanation: Size of an empty class is not zero. It is 1 byte generally. It is nonzero to ensure that the two different objects will have different addresses.



12. What is the output of this program?

        

Note:Includes all required header files

using namespace std;

 

class Empty { };

 

class Derived: Empty { int a; };

 

int main()

{

    cout << sizeof(Derived);

    return 0;

}

A. 1
B. 2
C. 4
D. Error

View Answer

Ans : C

Explanation: The output is not greater than 4. There is an interesting rule that says that an empty base class need not be represented by a separate byte. So compilers are free to make optimization in case of empty base classes



13. What is the output of this program?

      

Note:Includes all required header files

class Test

{

  int x;

};

 

int main()

{

  Test t;

  cout << t.x;

  return 0;

}

A. 0
B. Garbage value
C. Runtime error
D. Complier error

View Answer

Ans : D

Explanation: In C++, the default access is private. Since x is a private member of Test, it is compiler error to access it outside the class.



14. Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no alignment in objects. Predict the output following program.

        

Note:Includes all required header files

using namespace std;

 

class Test

{

    static int x;

    int *ptr;

    int y;

};

 

int main()

{

    Test t;

    cout << sizeof(t) << " ";

    cout << sizeof(Test *);

}

A. 12 12
B. 12 4
C. 8 4
D. 8 8

 

View Answer

Ans : C

Explanation: For a compiler where pointers take 4 bytes, the statement ""sizeof(Test *)"" returns 4 (size of the pointer ptr). The statement ""sizeof(t)"" returns 8. Since static is not associated with each object of the class, we get (8 not 12).



 

 

 

 

 

 

15. Which of the following is true about the following program

#include <iostream>

using namespace std;

class LFC

{

public:

    int i;

    void get();

};

void LFC::get()

{

    std::cout << "Enter the value of i: ";

    std::cin >> i;

}

LFC t; 

int main()

{

    LFC t; 

    t.get();

    std::cout << "value of i in local t: "<<t.i<<'\n';

    ::t.get(); 

    std::cout << "value of i in global t: "<<::t.i<<'\n';

    return 0;

}

A. Compiles and runs fine
B. Compiler Error in Line "::t.get();"
C. Compiler Error: Cannot have two objects with same class name
D. Runtime error

 

View Answer

Ans : A

Explanation: The above program compiles & runs fine. Like variables it is possible to create 2 objects having same name & in different scope.

 

 

 



16. What will be the output of this program?

        

Note:Includes all required header files

using namespace std;

 

class sample

{

    int x;

}

 

int main()

{

    sample obj;

    obj.x=100;

    cout<<"x="<< obj.x;

}

A. 10
B. 100
C. Error
D. None of the above

Ans : C

Explanation: By default, class data members and member functions are private, and we cannot access private members outside of the class.

 

17. What will be the output of this program?

       

Note:Includes all required header files

using namespace std;

//Empty class

class test

{

};

 

int main()

{

   test testObj;

   cout<<"size ="<< sizeof(testObj);

   return 0;

}

A. Error
B. size =Garbage
C. size =1
D. Compile but no output

 

View Answer

Ans : C

Explanation: size =1 An empty class object takes 1 byte in the memory, but it is not fixed it can be take any non zero value depending on the compiler and class definition.



18. What will be the output of the following program?

#include <iostream>

using namespace std;

class LFC

{

    public:

      int x;

};

int main()

{

    LFC *p = new LFC();

 

    (*p).x = 5;

    cout<< (*p).x << " " << p->x << " " ;

 

    p->x = 10;

    cout<< (*p).x << " " << p->x ;

    return 0;

}

A. 5 5 10 10
B. Garbage garbage 10 10
C. 5 5 Garbage garbage
D. Garbage garbage Garbage garbage

 

View Answer

Ans : A

Explanation: 5 5 10 10 will be the output of the following program.

19. Which of the following statements is correct when a class is inherited publicly?

A. Public members of the base class become protected members of derived class.
B. Public members of the base class become private members of derived class.
C. Private members of the base class become protected members of derived class.
D. Public members of the base class become public members of derived class.

View Answer

Ans : D

Explanation: Public members of the base class become public members of derived class is correct

20. What does the cerr represent?

A. Standard error stream
B. Standard logging stream
C. Input stream
D. Output stream

View Answer

Ans : A

Explanation: cerr is an object of class ostream that represents the standard error stream. It is associated with the cstdio stream stderr

21. Constructor is executed when _____.

A. An object goes out of scope.
B. A class is declared
C. An object is created
D. An object is used

View Answer

Ans : C

Explanation: Constructor is executed when An object is created.

22. How many ways of reusing are there in class hierarchy?

A. 1
B. 3
C. 4
D. 2

View Answer

Ans : D

Explanation: Class hierarchies promote reuse in two ways. They are code sharing and interface sharing.

23. Where does the object is created?

A. Class
B. Constructor
C. Destructors
D. Attributes

View Answer

Ans : A

Explanation: In class, only all the listed items except class will be declared.

24. Which of the following is a valid class declaration?

A. Class A { int x; };
B. Class B { }
C. Public class A { }
D. Object A { int x; };

View Answer

Ans : A

Explanation: Class A { int x; }; is a valid class declaration.

25. Which of the following is not correct (in C++) ?i) Class templates and function templates are instantiated in the same way ii) Class templates differ from function templates in the way they are initiated iii) Class template is initiated by defining an object using the template argument iv) Class templates are generally used for storage classes.

A. i
B. i & ii
C. ii ,iii, iv
D. iv

View Answer

Ans : C

Explanation: In C++ class template and function template are similar in the way the are initiated. Class template are not used for storage class. Class templates and function templates are instantiated in the same way and Class template is not initiated by defining an object using the template. So (2), (3), (4) are not correct in C++.

26. Which of the following keywords is used to control access to a class member?

A. Default
B. Break
C. Protected
D. Asm
 

View Answer

Ans : C

Explanation: Protected keywords is used to control access to a class member

27. Which of the following statements is incorrect?

A. Destructor of base class should always be static
B. Destructor of base class should always be virtual.
C. Destructor of base class should not be virtual.
D. Destructor of base class should always be private.

View Answer

Ans : B

Explanation: Destructor of base class should always be virtual statements is incorrect

28. Which operator can not be overloaded?

A. +
B. ::
C. -
D. *

View Answer

Ans : B

Explanation: :: operator can not be overloaded

29. When Virtual Table is created?

A. Every Class has VTable
B. Class inherited from other Class
C. Class has atleast one Virtual Function
D. When a Class Overrides the function of Base class

View Answer

Ans : C

Explanation: When Virtual Table is created Class has atleast one Virtual Function

30. What is the size of empty class?

A. 0
B. 2
C. 4
D. 1

 View Answer

Ans : D

Explanation: When we create object of empty class at that time State of that object is nothing. Behaviour of that object is also nothing, but compiler assigns a unique address to that object. Memory in Computer is always organized in the form of bytes and minimum memory available at object address location is 1 byte. That's why size of object of empty class is 1 byte.

31. How to access the object in the class?

A. Ternary operator
B. Scope resolution operator
C. Direct member access operator
D. None of the above

 

View Answer

Ans : C

Explanation: Objects in the method can be accessed using direct member access operator which is (.).

32. When struct is used instead of the keyword class means, what will happen in the program?

A. Access is public by default
B. Access is private by default
C. Access is protected by default
D. None of the mentioned

 

View Answer

Ans : A

Explanation: Access is public by default will happen When struct is used instead of the keyword class.

33. Which of the following is not a member of class?

A. Static Function.
B. Friend Function
C. Const Function
D. Virtual Function

View Answer

Ans : B

Explanation: Friend function is not a member of class.

 

32 

What happens when we try to compile the class definition in following code snippet?

 

class Birds {};

class Peacock : protected Birds {};

 

A.

It will not compile because class body of Birds is not defined.

B.

It will not compile because class body of Peacock is not defined.

C.

It will not compile because a class cannot be protectedly inherited from other class.

D.

It will compile succesfully.

 

33. Which of the following is not type of class?
a) Abstract Class
b) Final Class
c) Start Class
d) String Class

Answer: c
Explanation: Only 9 types of classes are provided in general, namely, abstract, final, mutable, wrapper, anonymous, input-output, string, system, network. We may further divide the classes into parent class and subclass if inheritance is used.

34. Class is pass by _______
a) Value
b) Reference
c) Value or Reference, depending on program
d) Copy

Answer: b

Explanation: Classes are pass by reference, and the structures are pass by copy. It doesn’t depend on the program.

35. What is default access specifier for data members or member functions declared within a class without any specifier, in C++?
a) Private
b) Protected
c) Public
d) Depends on compiler

Answer: a
Explanation: The data members and member functions are Private by default in C++ classes, if none of the access specifier is used. It is actually made to increase the privacy of data.

35. Which is most appropriate comment on following class definition?

classStudent

{

    int a;

    public : float a;

};

a) Error : same variable name can’t be used twice
b) Error : Public must come first
c) Error : data types are different for same variable
d) It is correct

Answer: a
Explanation: Same variable can’t be defined twice in same scope. Even if the data types are different, variable name must be different. There is no rule like Public member should come first or last.

36. Which is known as a generic class?
a) Abstract class
b) Final class
c) Template class
d) Efficient Code

Answer: c
Explanation: Template classes are known to be generic classes because those can be used for any data type value and the same class can be used for all the variables of different data types.

37. Size of a class is _____________
a) Sum of the size of all the variables declared inside the class
b) Sum of the size of all the variables along with inherited variables in the class
c) Size of the largest size of variable
d) Classes doesn’t have any size

Answer: d
Explanation: Classes doesn’t have any size, actually the size of object of the class can be defined. That is done only when an object is created and its constructor is called.

38. Which class can have member functions without their implementation?
a) Default class
b) String class
c) Template class
d) Abstract class

Answer: d
Explanation: Abstract classes can have member functions with no implementation, where the inheriting subclasses must implement those functions.

39. Which of the following describes a friend class?
a) Friend class can access all the private members of the class, of which it is a friend
b) Friend class can only access protected members of the class, of which it is a friend
c) Friend class don’t have any implementation
d) Friend class can’t access any data member of another class but can use it’s methods

Answer: a
Explanation: A friend class can access all the private members of another class, of which it is a friend. It is a special class provided to use when you need to reuse the data of a class but don’t want that class to have those special functions.

40. What is the scope of a class nested inside another class?
a) Protected scope
b) Private scope
c) Global scope
d) Depends on access specifier and inheritance used

Answer: d
Explanation: It depends on the access specifier and the type of inheritance used with the class, because if the class is inherited then the nested class can be used by subclass too, provided it’s not of private type.

41. Class with main() function can be inherited.
a) True
b) False

Answer: a
Explanation: The class containing main function can be inherited and hence the program can be executed using the derived class names also in java.

42. Which among the following is false, for a member function of a class?
a) All member functions must be defined
b) Member functions can be defined inside or outside the class body
c) Member functions need not be declared inside the class definition
d) Member functions can be made friend to another class using the friend keyword

Answer: c
Explanation: Member functions must be declared inside class body, though the definition can be given outside the class body. There is no way to declare the member functions outside the class.

43. Which syntax for class definition is wrong?
a) class student{ };
b) student class{ };
c) class student{ public: student(int a){ } };
d) class student{ student(int a){} };

Answer: b
Explanation: Keyword class should come first. Class name should come after keyword class. Parameterized constructor definition depends on programmer so it can be left empty also.

44. Which of the following pairs are similar?
a) Class and object
b) Class and structure
c) Structure and object
d) Structure and functions

Answer: b
Explanation: Class and structure are similar to each other. Only major difference is that a structure doesn’t have member functions whereas the class can have both data members and member functions.

45. Which among the following is false for class features?
a) Classes may/may not have both data members and member functions
b) Class definition must be ended with a colon
c) Class can have only member functions with no data members
d) Class is similar to union and structures

Answer: b
Explanation: Class definition must end with a semicolon, not colon. Class can have only member functions in its body with no data members.

46. Instance of which type of class can’t be created?
a) Anonymous class
b) Nested class
c) Parent class
d) Abstract class

Answer: d
Explanation: Instance of abstract class can’t be created as it will not have any constructor of its own, hence while creating an instance of class, it can’t initialize the object members. Actually the class inheriting the abstract class can have its instance because it will have implementation of all members.

download

Comments

Popular posts from this blog

Getting started with c programming (step-by-step) explanation

22620 Network and Information Security NIS solved lab manual pdf

22413 software engineering notes [ ⚠️] pdf | Msbte 4th semester diploma