This document contains a quiz with multiple choice and short answer questions about C++ concepts like classes, constructors, destructors, function templates, inheritance, pointers, streams, access control, and friends. There are 7 multiple choice questions worth 1-2 marks each and 4 short answer questions worth 3 marks each about constructors, streams, access control, and friends. The total marks for the quiz are 30.
1 of 2
Download to read offline
More Related Content
Mi 103 mi 103 quiz 2 (1)
1. Quiz 2 MI – 103 Duration: 30 min
Full Marks: 30
Roll No.:
Rules: Answer in brief; Do not discuss with your friend during the test; Use the space provided in the
question paper for answer; Extra answer sheet will not be given.
1. Define a class Word which can hold a word where the length of the word is specified in the constructor.
Implement the constructor and the destructor. (Maximum 30 lines) 10 Marks
Ans. #include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class Word
{
char *word;
int length;
public:
Word()
{
length=0;
word=new char[length+1];
}
Word(char *s)
{
length=strlen(s);
word=new char[length+1];
strcpy(word,s);}
void display()
{ cout<<word<<"n";
}
};
void main()
{
char *w1="Girl";
Word word1(w1);
Word word2("Good");
word1.display();
word2.display();}
2. 2. Function templates can accept
(A) any type of parameters (B) only one parameter (C) only parameters of the basic type
(D) only parameters of the derived type 2 Mark
Ans.C
3. How many constructors can a class have? (A) 0 (B) 1 (C) 2 (D) any number 1 Mark Ans.D
4. Which of the following is the valid class declaration header for the derived class d with
base classes b1 and b2?
(A) class d : public b1, public b2 (B) class d : class b1, class b2 (C) class d : public b1, b2
(D) class d : b1, b2 1 Mark
Ans.A
5. Declaration of a pointer reserves memory space (A) for the object. (B) for the pointer.
(C) both for the object and the pointer. (D) none of these. 2 Mark
Ans.B
6. If there is a pointer p to object of a base class and it contains the address of an object of a
derived class and both classes contain a virtual member function abc(), then the statement
p->abc(); will cause the version of abc() in the ____class to be executed. (A) Base Class
(B) Derived class (C) Produces compile time error (D) produces runtime error. 2 Mark
Ans.B
7. Write short note (most important three lines) on
(i) Constructor 3 Marks
A constructoris a member function which has the same name as at that of the class.
It is used to initialize the data members of the class.
A constructoris automatically invoked by the compiler when an object is created.
(ii) Stream 3 Marks
Stream is defined as an interface supplied by the I/O systemto the programmer and that which is
independent of the actual device being used.
These are basically of two types:one is when it is a source through which input can be taken and the other
is when it acts as a destination to which output can be sent.
The source stream is called the input stream and the destination streamis called the output stream.
(iii) Access control 3 Marks
This is done using access specifiers public, private and protected.
These access specifiers determine how elements of the base class are inherited by the derived.
If no specifier is used the data member is private by default.
(iv) Friend 3 Marks
Friend is keyword in C++ which is used to declare friend function and friend classes.
It can access the private data members of the class it has to take the help of the object of that class.
A friend function is not the member of the class.
Signature of the student: