Class and Object


12
Question 1
What is the difference between struct and class in C++?
A
All members of a structure are public and structures don't have constructors and destructors
B
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 deriving a class, default access specifier is private.
C
All members of a structure are public and structures don't have virtual functions
D
All of the above
Class and Object    
Discuss it


Question 1 Explanation: 
Question 2
Predict the output of following C++ program
#include<iostream>
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
Class and Object    
Discuss it


Question 3
class Test {
    int x; 
};
int main()
{
  Test t;
  cout << t.x;
  return 0;
}
A
0
B
Garbage Value
C
Compiler Error
Class and Object    
Discuss it


Question 3 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.
Question 4
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 the above
Class and Object    
Discuss it


Question 4 Explanation: 
Every 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. For example, every student object uses same logic to find out grades or any other method.
Question 5
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.
#include<iostream>
using namespace std;

class Test
{
    static int x;
    int *ptr;
    int y;
};

int main()
{
    Test t;
    cout << sizeof(t) << " ";
    cout << sizeof(Test *);
}
A
12 4
B
12 12
C
8 4
D
8 8
Class and Object    
Discuss it


Question 5 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).
Question 6
Which of the following is true about the following program
#include <iostream>
class Test
{
public:
    int i;
    void get();
};
void Test::get()
{
    std::cout << "Enter the value of i: ";
    std::cin >> i;
}
Test t;  // Global object
int main()
{
    Test t;  // local object
    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;
}
Contributed by Pravasi Meet
A
Compiler Error: Cannot have two objects with same class name
B
Compiler Error in Line "::t.get();"
C
Compiles and runs fine
Class and Object    
Discuss it


Question 6 Explanation: 
The above program compiles & runs fine. Like variables it is possible to create 2 objects having same name & in different scope.
Question 7
A member function can always access the data in __________ , (in C++).
A
the class of which it is member
B
the object of which it is a member
C
the public part of its class
D
the private part of its class
Class and Object    C++ Misc    UGC-NET CS 2017 Nov - II    
Discuss it


Question 7 Explanation: 
A member function can access it's class member variables, irrespective of the access specifier in which the member variable is declared.So, a member function can always access the data in the class of which it is a member. So, option (A) is correct.
Question 8
Which of the following is not correct for virtual function in C++ ?
A
Must be declared in public section of class.
B
Virtual function can be static.
C
Virtual function should be accessed using pointers.
D
Virtual function is defined in base class.
Class and Object    C++ Misc    UGC-NET CS 2017 Nov - II    
Discuss it


Question 8 Explanation: 
Virtual function is can't be static in C++. So, option (B) is correct.
Question 9
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
A
(1)
B
(2), (4)
C
(2), (3), (4)
D
(4)
Class and Object    C++ Misc    UGC-NET CS 2017 Nov - II    
Discuss it


Question 9 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++. So, option (C) is correct.
Question 10
Which of the following cannot be passed to a function in C++ ?
A
Constant
B
Structure
C
Array
D
Header file
Class and Object    UGC NET CS 2017 Jan - II    
Discuss it


Question 10 Explanation: 
Header file can not be passed to a function in C++. While array, constant and structure can be passed into a function. So, option (D) is correct.
There are 17 questions to complete.
12


My Personal Notes arrow_drop_up