Anda di halaman 1dari 44

CHAPTER 1: PRINCIPLES OF OBJECT – ORIENTED PROGRAMMING

1. Which of the following languages is not a pure Object-Oriented language?


a) Java
b) Simula
c) Ada
d) Eiffel

2. Which of the languages is a pure object-oriented language?


a) C++
b) Smalltalk
c) Object Pascal
d) Objective C

3. Which of the following statements is TRUE with respect to object-oriented


programming?
a) Emphasis is on algorithms rather than data.
b) Data can move freely from function to function.
c) Programs are divided into small programs called functions.
d) Data and functions that operate on them are tied together.

4. Which of the following issues is considered as the major drawback of the procedure –
oriented programming?
a) Employs tap-down approach in program design.
b) Emphasis is an algorithms.
c) Most of the functions share global data.
d) Large programs are divided into functions.

5. One of the major advantages of object-oriented programming approach is:


a) It can easily map real-world problems.
b) Data can move freely around the system.
c) Any language can be used for programming.
d) Structured programming concept can be easily incorporated.

6. The wrapping up of data and functions into a single unit is known as


a) Function overloading
b) Static binding
c) Abstraction
d) Encapsulation

7. The process by which objects of one class can acquire the attributes of objects of
another class is known as
a) Attribute passing
b) Inheritance
c) Abstraction
d) Polymorphism
8. Which one of the following concepts enables the reusability of code?
a) Dynamic binding
b) Polymorphism
c) Inheritance
d) Encapsulation

9. The term function over loading refers to:


a) Message passing
b) Dynamic binding
c) Data hiding
d) Polymorphism

10. Which of the following characteristics C++ does not support?


a) Persistence
b) Generacity
c) Garbage collection
d) Late binding

11. Encapsulation ensures that


a) any function in the program can access the data
b) external functions cannot access an object’s nonpublic data
c) external functions cannot access any data of the object
d) the data of the object is never modified

12. A rectangle notation that is used to denote an object usually show


a) communication links with other objects
b) only data members of the object
c) data and functions of the object
d) relationship with other objects
CHAPTER 2: BEGINNING WITH C++

1. C++ is an extension of C language. One of the new features added is:


a) Structure
b) Union
c) Operator overloading
d) Function

2. Which of the following statements is FALSE?


a) In a C++ program, the execution begins at main() function.
b) Every C++ program must have a main() function.
c) Every C++ statement must end with a semicolon.
d) C++ does not permit the use of printf() for displaying an output.

3. Line commands are inserted into a C++ program using


a) Left and right curly brackets like {…}
b) Double forward slashes like //…..
c) Stars and slashes like /*..…*/
d) A colon like this : …..

4. For declaring the variables a and b, which of the following declaration statements is
incorrect?
a) int a, b; // Declaration
b) int a, /* Declaration*/ b;
c) int a, // Declaration // b;
d) int a, b; // Declaration //

5. For using functions that convert numbers to text and text to numbers, we must include
the following header file:
a) <cstdlib>
b) <iostream>
c) <cstdio>
d) <cmath>

6. Which of the following statements provide two lines of output?


a) cout << ”VERY”
<< “GOOD”;
b) cout << ”VERY” “\n” << “GOOD” “\n”;
c) cout << “VERY”
<< “GOOD” “\n”;
d) cout << ”\nVERY” // First line
<< “GOOD”; // Second line
7. Which of the following statements is invalid in C++?
a) cin >> count;
b) cin >> x; >>y;
c) cin >> x
>> y;
d) cin >> x >>y;

8. Which of the following statements requires the header file <math.h> to be included?
a) cout <<x/y;
b) x + = 10;
c) x = m*n+1;
d) x = sqrt(y);

9. Name the header file that is to be used in the #include directive when we use cout for
displaying outputs.
a) <cstdio>
b) <cstdlib>
c) <iostrean>
d) <cassert>

10. If a is an object of the class B and void print (void) is a member function of B, then
which one of the following statements will invoke the function?
a) a::print();
b) a.voidprint();
c) B.print();
d) a.print();
CHAPTER 3: TOKENS, EXPRESSIONS AND CONTRAL STRUCTURES

1. Which one of the following is an ANSI C keyword?


a) public
b) operator
c) export
d) volatile

2. Which one of the following is not a C++ keyword?


a) constant
b) false
c) explicit
d) throw

3. Which of the following statements is TRUE with regard to naming C++ identifiers?
a) A name can start with any letter or digit.
b) No special characters are permitted.
c) Uppercase and lowercase letters are distinct.
d) A name cannot have more than 32 characters

4. Which of the following is a valid C++ identifier?


a) Total value
b) 2x
c) _string1
d) template

5. Which of the following is not a valid literal constant in C++?


a) “ANSI C++”
b) 0X5
c) 12, 345
d) 9.234L

6. Which of the following is a user-defined data type in C++?


a) array
b) enumeration
c) void
d) function

7. Which of the following is not a built-in data type in C++?


a) structure
b) void
c) char
d) long double
8. Which of the following program segments is valid in C++?
a) void *p1;
char *p2;
p2 = p1;

b) void *p1;
int *p2;
*p2 = *p1;

c) void *p1;
char *p2;
p2 = (char *) p1;

d) enum colour {red, blue};


colour background;
background = 1;

9. Assumming that we are working an a 16-bit word machine, which of the following
takes a size of 8 bytes
a) signed long int
b) long int
c) long double
d) double

10. Which of the following declaration statements is illegal?


a) const int m = 10;
b) const int count;
c) const m = 100;
d) int const *p = &x;

11. Which of the following statements is FALSE?


a) All variables must be declared before they are used
b) All variables must be given a type when they are declared
c) A variable can be declared anywhere in the scope
d) A variable must be initialized at the time of its declaration

12. Identify the error, if any, in the statement:


long sum = m1 + m2;
a) Declaration cannot contain any expression
b) Initialization at the time of declaration is not valid.
c) Type specification is incomplete.
d) No error
13. Which of the following statements is invalid?
a) int p = new int ;
b) int *p = new int ;
c) int *p = new int [10] ;
d) int *p = new int [m] [10] ;

14. For using the manipulator setw(), we must include the header file:
a) <iostream>
b) <cstdio>
c) <iomanip>
d) <cstdlib>

15. The following statements implement type cast operation. Which one of them is illegal?
a) x = y/(float)m;
b) x = y/float(m);
c) x = float(y)/(float)m;
d) x = y/float m;

16. The statement


a=(b=10) – 5;
is an example of
a) Chained assignment
b) Embedded assignment
c) Compound assignment
d) None of the above

17 Which one of the following expressions is an example of bitwise expression?


a) x>=y
b) x==10 && y<5
c) x << 10
d) x + = 10

18 Which of the following operators in C++ can be overloaded?


a) Conditional operator (?:)
b) Scope resolution operator (::)
c) Member access operator (.*)
d) Relational Operator (<=)

19. In which of the following groups all the operators are in the same level of
precedence?
a) <<, < =, = =, << =
b) new, sizeaf, typeid, !
c) new, delete, ->, ::
d) %, *, ->*, /=
20 Which of the following loop structures is known as an exit-controlled loop?
a) do… while loop
b) while loop
c) for loop
d) None of the above
CHAPTER 4: FUNCTIONS IN C++

1. Which one of the following statements is a valid main function prototype?


a) void main ( );
b) int main (int a, int b)
c) main (int argc, char argv);
d) int main(int argc, char* argv[ ]);

2. Which one of the following is true about a function in a C++ program?


a) A function must be called at least once.
b) A function may called as and when required.
c) A function can be called only by the main function.
d) A function must always return a value.

3. Which of the following function prototypes is invalid?


a) void exchange (int, int);
b) void exchange (int a, int b);
c) void exchange (int x, y);
d) void exchange (float, int);

4. Find the error in the following function prototype


double area (int)

a) Argument name is not specified


b) Semicolon at the end is missing
c) Return type and argument type must be same
d) No error

5. In C++, the main function returns to the operating system


a) an integer value
b) a floating point value
c) address of the function
d) none of the above

6. The ‘call by reference’ approach of invoking a function enables us to


a) copy the values of arguments into the calling function
b) not to alter the values of the original variables in the calling program
c) change the values of the variables in the calling program
d) use the function on the left-hand side of an assignment statement
7. Following is a list of function declarations with default values. Which one of them is
a valid declaration?
a) float mul(float a=5.0, b=10.0);
b) float mul(int a, float b=10.0, int c);
c) float mul(int a=5, float b);
d) float mul(int a, float b=10.0);

8. The following is the list of function declarations. Which one of them will be invoked
by the function call statement
cout <<mul(10.5, 10);

a) int mul(int a, int b);


b) double mul(float a, float b);
c) double mul(int a, double b);
d) int mul(float a, int b);

9. The usage of macros in C may be replaced in C++ by


a) friend functions
b) inline functions
c) virtual function
d) functions with default arguments

10. The function


mul(void)
{
return(5.5 * 3);
}
when executed will return
a) 16
b) 16.5
c) 16.0
d) error

11. Which of the following statements is true in respect of a function containing const
arguments?
a) The function cannot return a value.
b) The function cannot change the value of const arguments.
c) const should be used for the argument passed by reference.
d) const argument cannot be used in any expression.
12. Which of the following will return the value –8.0 given the value of x to be –8.5?
a) fabs(x)
b) ceil(x)
c) floor(x)
d) all the above
CHAPTER 5: CLASSES AND OBJECTS

1. C++ supports both classes and structures. Which of the following statements is
FALSE in respect of both of them?
a) Both can have variables and functions as members.
b) In both, the members are private by default.
c) They support inheritance.
d) Their names can be used like any other types names.

2. In a class definition, the members declared as private are accessible


a) only to the public functions of the class
b) to any function in the program
c) only to functions declared as private
d) to all member functions of the class

3. By default, member functions of a class become


a) private
b) public
c) protected
d) inline

4. In a class, a member declared as public is accessible


a) only to public members of the class
b) only to member functions of the class
c) to any function in the program
d) without using objects of the class

5. The dot operator is used to connect


a) a class and a member of that class
b) a class object and a member of that class
c) a class object and any function
d) a class and an object of that class

6. Declaration of members as private in C++ enables it to implement the concept


a) polymorphism
b) modularity
c) abstraction
d) encapsulation

7. Listed below are some of the possible characteristics of member functions. Which
one of them is NOT TRUE?
a) All the member functions should be defined within the class itself
b) Several different classes can use the same function name
c) Member functions can access the private members of the class.
d) A member function can call another member function directly without using
the dot operator.
8. State which of the following is false with respect to classes:
a) They permit data to be hidden from other parts of the program
b) They can directly map objects in the real world
c) They bring together all attributes of an entity in one place
d) They are removed from memory when not in use

9. Which one of the following statements is FALSE?


a) A member function defined outside can be made inline using the qualifier
inline.
b) An object of a class can access all its member functions whether they are
declared private or public.
c) A member function can be called by using its name alone inside another
member function.
d) A private member function can only be called by another function that is a
member of its class.

10. When called by its object, a member function declared as const


a) can modify data of only const members.
b) can modify data of only non-const members.
c) can modify data of all members.
d) cannot alter any data in the class.

11. A member function declared as static


a) can have access to only other static members of its class
b) must be called using only the objects of its class
c) can be called using only its class name
d) can have access to all members in the same class

12. Which one of the following statements is not true for a member function declared as
friend?
a) It can be invoked like a normal function without help of any object.
b) It cannot access the members of the class directly.
c) It cannot have objects as arguments.
d) It cannot be called using the objects of its class.

13. Which of the following is TRUE about static member variable of a calss?
a) Each object creates its own copy.
b) It is visible only within the class.
c) It can be initialized whenever necessary.
d) It cannot be assigned any initial value at the time of definition.

14. Which one of the following is TRUE while passing objects as function arguments?
a) A copy of the entire objects can be passed.
b) Only the address of the object can be passed.
c) A nonmember function can use objects as its arguments.
d) All of the above.
15. Which one of the following is FALSE?
a) The address of a class member can be assigned to a pointer variable.
b) When making a member function const, the qualifier const must be used in
both declaration and definition.
c) A class can be defined inside a function.
d) None of the above.
CHAPTER 6: CONSTRUCTORS AND DESTRUCTORS

1. Which of the following is NOT TRUE about constructors and destructors?


a) They must the same names as class.
b) They cannot return values.
c) They can be invoked only once in a program.
d) They are called automatically.

2. Which one of the following statements does correctly describe one of the many
characteristics of constructors?
a) They can be inherited.
b) They can return values, like other functions.
c) They can be declared anywhere in the class.
d) They can have default arguments.

3. A constructor for the class Area having two data members length and breadth is
declared as follows:
Area (int length = 15, int breadth = 10);
Which one of the following object declarations is illegal?
a) Area A(20, 10);
b) Area A();
c) Area A = Area (20, 10);
d) Area A;

4. Which one of the following is an implicit constructor for the class ROOM having a
data member area?
a) Room ( ) { }
b) Room (int a) {area = a}
c) Room (Room & a) {area = a.area}
d) None of the above

5. Which one of the following declations represents the legal copy constructor for the
class X?
a) X (X);
b) X (&X);
c) X (X&);
d) X ( );
6. Identify error, if any, in the following code segment:
1. class Sample
2. {
3. int m;
4. Sample ( );
5. Sample (int);
6. };

e) Line 6 should not have semicolon


f) In line 5 parameter name is missing
g) In line 4, argument should be of type void
h) Constructors should be declared in public section

7. Which one of the following is TRUE about a copy constructor?


a) It is used for passing address of data members
b) It declares and initializes an object from another object
c) It is invoked automatically when an object the class is declared
d) We can pass the argument by value to it

8. Which of the following statements is TRUE?


a) A class can have only one constructor
b) A constructor must be declared for each class
c) A default constructor can only be supplied by the compiler
d) A constructor is automatically called each time an object is created

9. Which of the following statements is TRUE?


a) A destructor must be declare for each class
b) A class can have only one destructor
c) A destructor can have default arguments
d) Destructors are not useful when the class contains pointer data members

10. Which of the following statements is FALSE?


a) A class can have only default constructor
b) A default constructor can only be supplied by the compiler
c) A default constructor may be declared without any parameters
d) All parameters of a default constructor can have default values
CHAPTER 7: OPERATOR OVERLOADING

1. Operator overloading means


a) giving new meanings to the existing C++ operators
b) creating new C++ operators
c) changing the precedence of operators
d) non of the above

2. Which of the following operators cannot be overloaded?


a) Scope resolution operator (::)
b) sizeof operator
c) Member access operator (.*)
d) All of the above

3. Operator overloading is used to


a) provide encapsulation
b) support inheritance
c) support compile-time polymorphism
d) support run-time polymorphism

4. State which of the following statements is TRUE.


a) Operator functions never return a value.
b) Friend functions cannot be used for operator overloading.
c) An operator function must have at least one argument.
d) The overloaded operator must have at least one operand that is of user-defined
type.

5. State which of the following statements is FALSE.


a) Overloaded operators follow the syntax rules of the original operators.
b) Friend functions can be used to overload all the over loadable operators.
c) Unary operators overloaded by means of a member function take no explicit
argument
d) Binary arithmetic operator functions must explicitly return a value

6. When overloading binary operators using friend functions, the operator function uses
a) two explicit arguments
b) one implicit and one explicit arguments
c) one explicit argument
d) no arguments

7. When overloading binary operators using member functions, the operator functions
use
a) two explicit arguments
b) one explicit argument
c) one implicit and one explicit arguments
d) one implicit argument
8. We can use a constructor function for accomplishing
a) conversion from class type to class type.
b) conversion from basic type to class type
c) conversion from class type to basic type
d) non of the above

9. The casing operator function can be used to accomplish the conversion


a) class type to structure type
b) basic type to class type
c) float type to long type
d) class type to basic type

10. The casting operator function must


a) be a class member
b) not specify any return type
c) not have any arguments
d) All of the above
CHAPTER 8: INHERITANCE : EXTENDING CLASSES

1. The concept of inheritance enables C++


a) to pass arguments to objects of classes
b) to create new classes from existing classes
c) to improve data hiding
d) to support abstraction

2. What is the advantage of using inheritance in C++?


a) Facilitates creation of class libraries
b) Enables reuse of existing code
c) Enhances reliability of the code
d) All the above

3. Through inheritance, it is possible to inherit from a class


a) only data members
b) only members functions
c) any members as we desire
d) only functions declared as friend

4. In the class definition


class M : visibility-mode N
{
members of M
};
the visibility-mode
a) must be blank
b) must be public
c) must be private
d) may be blank, public or private

5. Consider the code segment


class A
{
private : int a ;
public : int b ;
protected : int c ;
};
class B : A { };
class C : public B { };
The derived class C would contain the members
a) a, b and c
b) b and c only
c) b only
d) none
6. Which of the following statements is TRUE, when a derived class D inherits a base
class B using protected specifier?
a) public members of B become protected members of D
b) public members of B become public members of D
c) public members of B become private members of D
d) public members are not inherited

7. Which of the following functions can have access to the protected data of a class?
a) A function that is friend of the class
b) A member function of a class declared friend of the class
c) A member function of a derived class
d) All the above

8. The use of scope resolution operator in inheritance enables us


a) to restrict the visibility of data members
b) to determine the base class of a derived class
c) to specify a particular class
d) to make a private member inheritable

9. The process of a class inheriting attributes from several classes is known as


a) multilevel inheritance
b) multiple inheritance
c) multipath inheritance
d) hybrid inheritance

10. A class D is privately derived from class B. An object of D in the main function can
access
a) public members of B
b) private members of D
c) protected members of B
d) public members of D

11. The following examples show that the class C is derived from classes A and B.
Which one of them is legal?
a) class C : private A, public B
b) class C :: private A, public B
c) class C : public A : public B
d) class C: class A, B
e) class C: private A, public B;
12. Consider the following code:
class A { };
class B : A { };
What happens when we compile this code?
a) Will not compile because the body of A is empty
b) Will not compile because the body of B is empty.
c) Will not compile because the visibility mode for A is not specified
d) Will compile successfully

13. Which one of the following statements is FALSE?


a) Deriving one class from another requires fundamental changes to the base
class
b) If no constructors are declared for the derived class, the objects of the derived
class will use the constructors in the base class
c) There may be situations where we may have to define a class which will never
be used to create objects
d) It is legal to make objects of one class as members of another class.

14. Consider the following class definition with inheritance:


class A : public B, virtual public C
{
};
The order of execution of constructors will be:
a) A ( ), B ( ), C ( )
b) C ( ), B ( ), A ( )
c) B ( ), C ( ), A ( )
d) A ( ), C ( ), B ( )

15. Consider the following class


class ABC
{
int a;
int b;
public : constructor function
};
We wish to define the constructor function using an initialization list. Which one of
the following is the legal constructor function?
a) ABC (int x, int y): a (x), b(y) { }
b) ABC (int x, int y): b (x), a(x+y) { }
c) ABC (int x, int y): a (x), b(a*y) { }
d) All the above
CHAPTER 9: POINTERS, VIRTUAL FUNCTIONS AND POLYMORPHISM

1. A pointer is
a) the data type of an address variable
b) a variable for storing addresses
c) a special type of integer variable
d) a special type of operator

2. The declaration statement


int* p1, p2 ;
a) declares two pointer variables p1 and p2
b) declares two integer variables p1 and p2
c) declares p1 as a pointer variable and p2 as an integer variable
d) is illegal because of blank space between * and p1

3. The statement
int * p ;
can be interpreted as
a) the variable whose address is stored in p is an integer
b) the variable pointed to by p is an integer
c) p points to an integer
d) All the above

4. For the statement


p = *m;
which of the following statements is TRUE?
a) The value of m is assigned to p
b) The value of the variable pointed to by m is assigned to p
c) p is a pointer variable
d) Address of m is assigned to p

5. Identy the error in the following code segment


int *ptr, m = 100; // line 1
cout << *ptr; //line 2
a) Declaring ptr and m in one line is wrong
b) In line 2, *ptr should be written as &ptr
c) The address of m should be assigned to ptr before it is accessed
d) No error

6. Compile-time polymorphism is accomplished by using


a) function overloading
b) Operator overloading
c) a and b
d) neither a nor b
7. The mechanism of using virtual functions to achieve polymorphism is known as
a) run-time polymorphism
b) dynamic binding
c) late binding
d) all the above

8. consider the following class definition:


class Test
{
public : void show (void) {cout <<”Test”;}
};
If x is an object of Test and ptr is a pointer to x, then which one of the following
statements can correctly invoke the function show ( )?
a) x.show ( ) ;
b) ptr -> show ( ) ;
c) (*ptr). show ( );
d) all the three

9. If x is a private data member of a class, then it is legal to assign a value to x inside a


member function using
a) x = 100;
b) this ->x = 100;
c) (*this).x = 100;
d) all the above

10. The statement


return * this ;
inside a member function of a class returns
a) a copy of the object that invoked the function
b) address of the member function
c) address of this pointer
d) None of the above

11. Which of the following statements is FALSE?


a) Address of a derived class object can be assigned to a base class pointer
b) Teating the base class object as a derived class object is wrong
c) Address of a base class object can be assigned to a derived class pointer
d) A base class pointer cannot be used to access all the members of the derived class

12. In C++, virtual functions are used to


a) create functions that can never be accessed
b) enable the use of the same function call to invoke functions from different
classes
c) to make a base class abstract
d) to create an array of base class pointers that can point to different functions
13. Which of the following statements is TRUE about virtual functions?
a) A constructor cannot be a virtual function
b) A virtual function in the base class must be defined, eventhough it may not be
used
c) They cannot be declared static
d) All the above

14. A pure virtual function is a virtual function that


a) makes the class to be abstract
b) is used in derived classes
c) is used to transform the base class into a virtual base class
d) takes no argument and returns nothing

15. Which of the following statements is TRUE?


a) All virtual functions in an abstract base class must be declared as pure virtual
functions
b) A class is made abstract by declaring it virtual
c) If a base class declares a pure virtual function, all derived classes must define
the function
d) A derived class cannot contain a pure virtual function
CHAPTER 10: MANAGING CONSOLE I/O OPERATIONS

1. Functions that are necessary for handling formatted input and output operations are
declared in the class
a) iostream
b) ios
c) istream
d) ostream

2. Which of the following statements is TRUE?


a) The extraction operator >>always skips white space characters
b) The cout stream is normally connected to the display screen
c) The function write ( ) outputs an entire line of text
d) All the above
e) None of the above

3. Which of the following statements is FALSE?


a) The put( ) function can be used to output a line of text using a loop construct
b) The get( ) can be used to read a character from keyboard including blank
space and new line character
c) The function getline( ) does not take any arguments
d) A C++ stream is associated with a particular class

4. Which of the following statements is illegal?


a) cin.get(‘x’);
b) cin.get(x);
c) x = cin.get( );
d) cout.put(‘x’);

5. If m and n represent integer values, which of the following statements is legal?


a) cout.write(text, 10);
b) cout.write(text, m);
c) cout.write(text1, m).write (text2, n);
d) All the above
e) None of the above

6. The ios format function precision ( ) is used to


a) specify required field size
b) specify the number of digits to be displayed after decimal point
c) specify the number of digits to be displayed of an integer number
d) display output in scientific format
7. The statements
cout.width(4);
cout<< 12 << 34 << “\n”;
will produce the output in the following format
a) 1 2 3 4

b) 1 2 3 4

c) 1 2 3 4

d) 1 2
3 4

e) 1 2
3 4

8. Which of the following statements is TRUE?


a) We need not specify the precision for each item separately
b) If the specified field width is smaller than the size of the value, C++ expands
the size of field to fit the value
c) When displaying floating point numbers using precision function, the trailing
zeros are truncated
d) When the function width ( ) is used, the text is printed right – justified
e) All the above

9. Which of the following is a valid statement?


a) cout.setf(ios:: internal, ios:: basefield):
b) cout.setf(ios:: scientific, floatfield);
c) cout <<setf(ios:: hex, ios:: basefield);
d) None of the above

10. For displaying plus sign and trailing zeros for printing positive floating point
numbers, we must use the flag
a) ios:: showbase only
b) ios:: showpos only
c) ios:: showpoint only
d) both a and b
e) both b and c
11. What would be the output of the following code segment?
cout.setf(ios:: showpos);
cout.precision(2);
cout<< 345.609

a) 345.60
b) +345.61
c) +345.6
d) +345.609
e) 345.61

12. Which of the following statements are legal?


(i) cout << setwidth (5);
(ii) cout.setprecision (3);
(iii) cout <<123.45 << setw (10);

a) i only
b) i and ii
c) i and iii
d) iii only
e) ii and iii
f) all the three
CHAPTER 11: WORKING WITH FILES

1. All file stream classes are derived from


a) filebuf
b) fstream
c) fstreambase
d) ios

2. Which of the following classes support simultaneous input and output file operations?
a) iostream
b) fstream
c) filebuf
d) None of the above

3. Which of the following is TRUE?


a) We must call close( ) function explicitly to close a file associated with an
fstream object
b) Always sequential files are updated without overwriting any data
c) Any existing file can be opened for writing only purpose
d) Searching all records in a random-access file to locate a record is not
necessary

4. Which of the following is FALSE?


a) A file can be opened only using the constructor function of the stream class
b) Some streams work with input, and some with output
c) The function open ( ) can be used to open multiple files using one stream
d) The open ( ) can take two arguments

5. When reading a file that is connected to the input stream fin of ifstream, we can
detect the end-of-file condition using
a) while (fin) statement
b) if (fin.eof ( )!= 0) statement
c) either of them
d) neither of them

6. To open an existing file to add data to the end of the file or to modify the existing
data anywhere in the file, the second argument of open ( ) must be
a) ios :: ate
b) ios :: app
c) ios :: trunc
d) ios :: noreplace
7. To write floating point data to a file, we must use
a) put ( ) function
b) write ( ) function
c) the insertion operator
d) seekp ( ) function

8. The statement
file1.write ((char*) &M, sizeof (M));
writes
a) the address of M to file1
b) the functions of M to file1
c) all the members of M to file1
d) data in object M to file1

9. Assuming that the length of the file f1 is 100 bytes, what will be the output of the
following code segment?
f1.seekg(0, ios::end);
f1.seekg(-10, ios::cur);
cout << f1.tellg ( );
a) 0
b) 100
c) 90
d) Error; no output

10. Any fatal error occurring during a file operation can be located by using the function
a) fail ( )
b) bad ( )
c) eof ( )
d) good ( )
CHAPTER 12: TEMPLATES

1. A template may be used to create a family of


a) different types of variables
b) classes
c) functions
d) classes and functions

2. C++ templates provide support for designing


a) modular programming
b) structured programming
c) generic programming
d) top-down programming

3. A class template is designed


a) to generate identical objects
b) to work with different data types
c) to generate classes with different functions
d) All of the above

4. Which one of the following is FALSE?


a) class templates are sometimes called parameterized classes
b) There can be more than one template arguments
c) A template argument is preceded by the keyword class
d) Templates automatically create different version of the function at run-time,
depending on user input

5. When we use the concept of templates, when does the program generate actual code
for a template function?
a) At the time of invoking the function
b) At the time of declaration of the function
c) When the function definition is made
d) When the function is executed at run-time

6. Given below is the definition of a function template


template [class T]
T mul (x, y)
{
return x * y;
}
Identify errors, if any.
a) class T in the first line should be within angle brackets < and >
b) The type of arguments of mul function must be declared as T
c) Both a and b
d) No error
7. Which of the following statements is TRUE?
a) Member functions of a template class defined outside the class need not be
defined as function templates
b) In class templates with multiple arguments, all the arguments need not be of
class T type arguments
c) Template functions can not be overloaded
d) All the above

8. Consider a function template as shown below:


template < class T >
int find (T*array, T n, int m)
{
for ( int i = o; i < m; i++)
if (array [i] = = n
return i;
return (-1);
}
Which one of the following prototype calls can generate a function?
a) find (int*, float, int);
b) find (T*, int, int);
c) find (int*, int, float);
d) find (float*, float, int);

9. As we know, a macro can be used to create different versions of a function for


different data type. What is then the difference between a macro and a class
template?
a) macros do not perform type checking
b) Macros do not specify the type of the value to be returned
c) Macros are suitable to functions that can written in a single statement
d) All the above

10. The following is a list of header lines for creating function templates. Which of them
is legal?
a) template < class T > swap ( T & x, T & y)
b) template < T > void swap (T & x, T & y)
c) class < template T> void swap (T & x, T & y)
d) None of the above
CHAPTER 13: EXCEPTION HANDLING

1. Exceptions are caused by


a) logic errors
b) syntax errors
c) both logic and syntax errors
d) anomalies that a program may encounter during execution

2. Which of the following events could throw an exception?


a) Division by zero
b) Array subscripts out of bounds
c) Shortage of memory space available to satisfy a new request
d) All of the above

3. Exceptions are thrown


a) from throw statement to try block
b) from throw statement to catch block
c) from catch block to try block
d) from the statement containing error to catch block

4. State which of the following is TRUE.


a) Statements that cause an exception must be in a catch block
b) A statement that causes an exception should be in a try block
c) Once an exception is thrown, the control cannot return to the throw point
d) A program cannot continue to operate after an exception has ocurred.

5. The catch block


a) must be placed immediately after the try block
b) must be placed just before the try block
c) must be placed immediately after the statement throwing exception
d) may be placed anywhere in the program

6. Which of the following are legal throw statements?


a) throw ( );
b) throw (exception);
c) throw exception ;
d) throw;
e) All of the above

7. When an exception occurs inside a catch block, what happens?


a) The exception is caught by the same catch block
b) The exception is caught by the next catch block in the group
c) It will be passed on to the next try / catch sequence for processing
d) The execution will terminate
8. The statement catch (…) that can catch all types of exception
a) can be placed anywhere in the program
b) can be placed anywhere in the catch group
c) must always be placed in the beginning of catch group
d) must always be placed in the last in the catch group

9. An exception thrown by a try block that is followed by a group of catch blocks,


a) is caught by the first catch statement in the group
b) is caught by the catch( ) whose argument matches with the type of exception
thrown
c) is always caught by catch (…) statement placed at the end
d) is caught by the catch statement in the next enclosing try / catch sequence

10. We may restrict the types of exception thrown from a function


a) by placing the try block in the function itself
b) by placing the try block in the main that invokes the function
c) by adding a throw (type-list) to the function definition
d) by adding an empty throw ( ) in the function header line
CHAPTER 14: STANDARD TEMPLATE LIBRARY

1. Majority of template classes and functions contained in STL were originally developed
by
a) Bjarne stroustrup
b) ANSI standards committee
c) International standards organization (ISO)
d) Alexander Stepanov and Meng Lee

2. To implement STL components in a program, we must include


a) using namespace std directive
b) <cstdlib> header file
c) <cstdio> header file
d) <functional> header file

3. An STL containers can be used to


a) compile C++ programs that use template classes
b) store objects of any data type
c) process data stored in memory
d) None of the above

4. Which one of the following is FALSE?


a) An iterator can be handled just like a pointer
b) One purpose of an iterator in STL is to connect algorithms and containers
c) STL algorithms are member functions of containers
d) STL algorithms operate on container elements indirectly using iterators

5. An STL algorithm is
a) a friend function of a container class
b) a member function a container class
c) a standalone function designed to operate on containers
d) None of the above

6. Which one of the following is TRUE?


a) Algorithms can be used only on STL containers
b) An iterator can always move forward or backward through a container
c) The back( ) member function removes the element at the back end of the
container
d) The member function end( ) returns a reference to the last element in the
container

7. Which one of the following is a sequence container?


a) deque
b) set
c) queue
d) None of them
8. A vector container
a) is a static array
b) allows insertions and deletions fast at back
c) allows insertions anywhere very fast
d) does not permit direct access to any element

9. Which one of the following is a non-mutating algorithm?


a) unique ( )
b) merge ( )
c) for_each ( )
d) partition ( )

10. Which of the following algorithm is used to replace an element with a specified
value?
a) generate ( )
b) replace_copy ( )
c) remove_copy ( )
d) replace ( )

11. In associative containers,


a) elements are stored sequentially
b) keys are used to access elements
c) random access is very fast
d) sort ( ) algorithm is used to sort the elements

12. A list container


a) permits the use of bidirectional iterators
b) supports only forward iterator
c) supports only input iterator
d) allows random access

13. A map container


a) does not allow deletion of any element
b) allows insertion only at the end
c) stores pairs of objects (or values)
d) associates multiple keys for a given value

14. To use the function object


modulus < int > ( )
in a program, we must include the header file
a) <map>
b) <functional>
c) <list>
d) <algorithm>
15. Consider the following code:
int main ( )
{
vector <int> x;
for (int i = 1; i<4; i++)
v.push_back(i);
v.push_back(7.6);
vector<int>:: iterator p = v.begin ( );
p = p + 1;
v.insert (p, 5);
v.erase (v.begin + 3);
return 0;
}
When executed, the vector will contain the elements
a) 1, 5, 2, 8
b) 1, 5, 3, 7
c) 1, 2, 5, 7
d) 1, 5, 2, 7
e) None of the above
CHAPTER 15: MANIPULATING STRINGS

1. Which one of the following is a string class constructor?


a) string (void);
b) string (const char * str);
c) string (string & str);
d) All of them

2. Which one of the following initialization statements is illegal?


a) string city1="Delhi";
b) string city1("Delhi");
c) string city2= "New" + city;
d) string city3(city1);
e) None of the above

3. Identify the error in the following statement


for(int i=1; i<=string1.length( );i++)
cout <<string1.at(i);

a) The second line should have opening and closing braces like {……}
b) Initial value for i should be zero
c) The relational operator should be < and not <=
d) string1.at(i) should be written as string1.at[i]

4. What will be the output of the following code segment?


int main ( )
{
string s1("Hello");
string s2("123");
s1.insert(4, s2);
s1.replace(2, 2, s2);
cout << s1;
return 0;
}
a) He123
b) Hel123
c) Hell123
d) H123o
e) None of the above

5. Which of the following functions is not supported by the string class?


a) capacity ( )
b) empty ( )
c) assign ( )
d) count ( )
6. Which of the following operators cannot be used for string objects?
a) []
b) &
c) +=
d) <<

7. Which of the following are TRUE for the statement


s1.compare(s2);

(i) returns 1, when s1 and s2 are equal


(ii) returns 0, when s1 and s2 are equal
(iii) returns –1, if s1 < s2
(iv) returns –1, if s1 > s2
(v) returns 1, if s1 < s2
(vi) returns 1, if s1 > s2

a) i, ii and vi
b) ii, iv and v
c) ii, iii and vi
d) i, iii and vi

8. To access individual characters, we must use the function


a) find ( )
b) substr( )
c) find_first_of ( )
d) at ( )

9. Which of the following is TRUE?


a) String objects are null terminated
b) Function end ( ) returns an iterator to the invoking objects
c) Elements of string objects are numbered from 0
d) String objects cannot be copied using assignment operator

10. To replace the contents of string s1 with that of s2 and contents of s2 with that of s1,
we can use the statement
a) s1.swap(s2);
b) swap(s1, s2);
c) s1.replace(s2)
d) replace(s1, s2);
CHAPTER 16: NEW FEATURES ANSI C++ STANDARD

1. One of the new data types introduced by ANSI C++ standard is


a) new
b) throw
c) bool
d) void

2. Which of the following operators is normally used to change a pointer type object to
integer type object
a) static_cast operator
b) reinterpret_cast operator
c) typeid operator
d) const_cast operator

3. Consider the following code segment:


int x=10, y=20;
bool b1, b2;
b1 = x = = y;
b2 = x < y;
bool b3 = 15;
y = x + y + b1 + b2 + b3
cout << y;
What will be the output on executing the above code?
a) Error; we cannot assign integer value to a Boolean variable
b) 30
c) 33
d) 32

4. Which of the following operators is used to obtain the type of an object?


a) const_cast
b) typeid
c) typedef
d) dynamic_cast
5. What will be the output of the following code, when executed?
…….
class X { };
class Y { };
int main ( )
{
X x1, x2;
Y y1, y2;
if (typeid(x1) = = typeid(y1))
cout <<"same types";
else
cout <<Different types";
}
a) Different types
b) Same types
c) Error; we cannot compare types of two objects
d) Error; typeid( ) should be written in the form typeid(object).name( )

6. Consider the code segment:


class Test
{
float x;
public:
explicit Test (float y)
{ x = y; }
};
What are the changes required in the definition of the class Test, if it is to be
implemented by the main ( ) as given below.
int main ( )
{
Test t1 = 35.65;
cout << x;
}
a. Data member x should be declared public
b. The keyword explicit should be removed
c. Both a and b
d. No changes required

7. We can modify the value of a data member of an object declared constant by


declaring it using the keyword
a) explicit
b) typeid
c) typename
d) mutable
8. The correct format for defining a namespace is:
a. namespace (namespace_name){…}
b. namespace namespace_name {…}
c. namespace namespace_name {…};
d. namespace ( ) namespace_name {…}

9. We use namespaces to
a) divide a long program into different modules
b) divide a program into separate files
c) restrict the scope of program elements
d) prevent any modification in data members

10. Given the namespace definition


namespace S
{
int count;
}
How can we access the variable count outside the namespace?
a) S::count = 10;
b) using namespace S; count = 10
c) using namespace :: count; count = 10
d) All of the above
CHAPTER 17: OBJECT-ORIENTED SYSTEM DEVELOPMENT

1. One of the fmost frequently used second generation development tools is


a) Inheritance graphs
b) Object dictionary
c) Data dictionary
d) Yourdan charts

2. The classic software development life cycle is usually represented by


a) Fountain model
b) Spiral model
c) Unified model
d) Water-fall model

3. The object-oriented approach uses heavily


a) structured charts
b) entity relationship charts
c) system flow charts
d) Grid charts

4. The fountain model of software development basically depicts


a) overlap and iteration between various stages
b) data flow in a system
c) control logic of functions
d) relationship between different modules of a system

5. The 'is a' relationship can be illustrated by


a) classification chart
b) hierarchical chart
c) composition chart
d) inheritance chart

6. The process of one object calling a member of another object may be represented by
a) hierarchical relationship
b) composition relationship
c) use relationship
d) inheritance relationship

7. Data flow diagrams are also known as


a) process charts
b) bubble charts
c) grid charts
d) flowcharts
8. Objects in a program may correspond to
a) procedures for solving problems
b) processes in a real_world problem
c) semantics in textual description
d) nouns in textual analysis

9. Which of the following types of verbs is likely to be a good candidate for


representing a member function?
a) Compare verbs
b) Being verbs
c) Stative verbs
d) Having verbs

10. To prepare a class hierarchy, we need to know


a) number of attributes in each class
b) number of functions in each class
c) common attributes and functions among class
d) use relationship

11. A class, in addition to service functions, may also contain


a) class management functions
b) class access functions
c) class utility functions
d) All the above

12. Which of the following statements is TRUE with regard to design of classes?
a) Interaction between two classes must be explicit
b) An object of one class should not send any message directly to a member of
another class
c) A class should be dependent on as few classes as possible
d) None of the above
e) All of the above

13. Which of the following statements is FALSE with regard to design of member
functions?
a) Each function either accesses or modifies some data of the class it represents
b) All functions must always be declared public
c) They can use top-down functional decomposition technique
d) They can use structured design techniques

14. The main ( ) function module known as the driver program


a) cannot receive data values from users
b) cannot display any output
c) creates only objects from class definitions
d) creates objects and establishes communication between them
15. Let a be an object of class and b of class B. A and B have some association between
them. Which of the following statements is TRUE?
i) Object a can send a message to b
ii) Object b can do some service to a
iii) One class must be a subclass another
iv) One object must be a member of another object

a) i and ii
b) iii and iv
c) All the four
d) None of the four

Anda mungkin juga menyukai