Structuring of Program: Unit III
Structuring of Program: Unit III
STRUCTURING OF PROGRAM
40. Which of the following is correct way of implementing an interface salary by class
manager?
(a) class manager extends salary {}
(b) class manager implements salary {}
(c) class manager imports salary {}
(d) None of the mentioned.
Answer : b
Explanation : None.
41. Which of the following is incorrect statement about packages?
(a) Interfaces specifies what class must do but not how it does.
(b) Interfaces are specified public if they are to be accessed by any code in the
program.
(c) All variables in interface are implicitly final and static.
(d) All variables are static and methods are public if interface is defined pubic.
Answer : d
Explanation : All methods and variables are implicitly public if interface is declared
public.
42. Which of the following package stores all the standard java classes?
(a) lang (b) java (c) util (d) java packages
Answer : b
Explanation : None.
43. What is the output of this program?
interface calculate {
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
x = item * item;
}
}
class interfaces {
public static void main(String args[]) {
display arr = new display;
arr.x = 0;
Unit III | 3.7
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) STRUCTURING OF PROGRAM
arr.cal(2);
System.out.print(arr.x);
}
}
(a) 0 (b) 2
(c) 4 (d) None of the mentioned
Answer : c
Explanation : None.
Output :
$ javac interfaces.java
$ java interfaces
4
44. What is the output of this program?
interface calculate {
void cal(int item);
}
class displayA implements calculate {
int x;
public void cal(int item) {
x = item * item;
}
}
class displayB implements calculate {
int x;
public void cal(int item) {
x = item / item;
}
}
class interfaces {
public static void main(String args[]) {
displayA arr1 = new displayA;
displayB arr2 = new displayB;
arr1.x = 0;
arr2.x = 0;
Unit III | 3.8
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) STRUCTURING OF PROGRAM
arr1.cal(2);
arr2.cal(2);
System.out.print(arr1.x + " " + arr2.x);
}
}
(a) 00 (b) 2 2 (c) 4 1 (d) 1 4
Answer : c
Explanation : class displayA implements the interface calculate by doubling the value
of item, where as class displayB implements the interface by dividing item by item,
therefore variable x of class displayA stores 4 and variable x of class displayB stores 1.
Output :
$ javac interfaces.java
$ java interfaces
41
45. What is the output of this program?
interface calculate {
int VAR = 0;
void cal(int item);
}
class display implements calculate {
int x;
public void cal(int item) {
if (item<2)
x = VAR;
else
x = item * item;
}
}
class interfaces {
public static void main(String args[]) {
display[] arr=new display[3];
for(int i=0;i<3;i++)
arr[i]=new display();
arr[0].cal(0);
arr[1].cal(1);
arr[2].cal(2);
System.out.print(arr[0].x+" " + arr[1].x + " " + arr[2].x); } }
(a) 012 (b) 0 2 4 (c) 0 0 4 (d) 0 1 4
Answer : c
Explanation : None.
Output :
$ javac interfaces.java
$ java interfaces
004
46. What will be the output of following program?
#include<iostream.h>
void main()
{
float x;
x=(float)9/2;
cout<<x;
}
(a) 4.5 (b) 4.0 (c) 4 (d) 5
Answer : a
47. The term _____ means the ability to take many forms.
(a) Inheritance (b) Polymorphism (c) Member function (d) Encapsulation
Answer : b
48. Runtime polymorphism is achieved by
(a) Friend function (b) Virtual function
(c) Operator overloading (d) Function overloading
Answer : b
49. Access to private data
(a) Restricted to methods of the same class
(b) Restricted to methods of other classes
(c) Available to methods of the same class and other classes
(d) Not an issue because the program will not compile
Answer : b
57. The actual source code for implementing a template function is created when
(a) The declaration of function appears.
(b) The function is invoked.
(c) The definition of the function appears.
(d) None of the above.
Answer : b
58. Usually a pure virtual function
(a) Has complete function body
(b) Will never be called
(c) Will be called only to delete an object
(d) Is defined only in derived class
Answer : d
59. 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
Answer : a
60. The process of extracting the relevant attributes of an object is known as
(a) Polymorphism (b) Inheritence
(c) Abstraction (d) Data hiding
Answer : b
61. What features make C++ so powerful ?
(a) Easy implementation (b) Reusing old code
(c) Reusing old code (d) All of the above
Answer : d
62. Which of the following operator can be overloaded through friend function?
(a) -> (b) = (c) ( ) (d) *
Answer : d
63. The keyword friend does not appear in
(a) The class allowing access to another class
(b) The class desiring access to another class
(c) The private section of a class
(d) The public section of a class
Answer : c
Unit III | 3.12
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) STRUCTURING OF PROGRAM
71. _____ members of a base class are never accessible to a derived class.
(a) Public (b) Private (c) Protected (d) (a), (b) and (c)
Answer : b
72. What is the error in the following code?
class t
{
virtual void print();
}
(a) No error
(b) Function print() should be declared as static.
(c) Function print() should be defined.
(d) Class t should contain data members.
Answer : a
73. It is possible to declare as a friend
(a) A member function (b) A global function
(c) A class (d) All of the above
Answer : d
74. A struct is the same as a class except that
(a) There are no member functions
(b) All members are public
(c) Cannot be used in inheritance hierarchy
(d) It does have a this pointer
Answer : c
75. C++ was originally developed by
(a) Clocksin and Melish (b) Donald E.Knuth
(c) Sir Richard Hadlee (d) Bjarne Stroustrup
Answer : d
76. What is the output of the following code
char symbol[3]={‘a’,‘b’,‘c’};
for (int index=0; index<3; index++)
cout << symbol [index];
(a) abc (b) “abc” (c) abc (d) ‘abc’
Answer : c
77. If we create a file by ‘ifstream’, then the default mode of the file is _____
(a) ios :: out (b) ios :: in (c) ios :: app (d) ios :: binary
Answer : b
78. The following can be declared as friend in a class
(a) An object
(b) A class
(c) A public data member
(d) A private data member
Answer : b
79. The polymorphism can be characterized by the phrase
(a) One interface,multiple methods
(b) Multiple interfaces,one method
(c) One interface,one method
(d) None of the above
Answer : a
80. A virtual class is the same as
(a) An abstract class (b) A class with a virtual function
(c) A base class (d) None of the above
Answer : d
81. Member functions, when defined within the class specification
(a) Are always inline
(b) Are not inline
(c) Are inline by default, unless they are too big or too complicated
(d) Are not inline by default.
Answer : a
82. Assume that we have constructor functions for both base class and derived class.
Now consider the declaration in main( ). Base * P = New Derived; in what sequence
will the constructor be called ?
(a) Derived class constructor followed by Base class constructor.
(b) Base class constructor followed by derived class constructor.
(c) Base class constructor will not be called.
(d) Base class constructor will not be called.
Answer : b
83. The operator that cannot be overloaded is
(a) ++ (b) : : (c) ~ (d) ( )
Answer : b
Unit III | 3.15
PRINCIPLES OF PROGRAMMING LANGUAGES (SE COMP.) STRUCTURING OF PROGRAM
98. What is the general syntax for accessing the namespace variable?
(a) namespaceid::operator (b) namespace,operator
(c) namespace#operator (d) none of the mentioned
Answer : a
Explanation : namespaceid::operator
99. Which keyword is used to access the variable in namespace?
(a) using (b) dynamic (c) const (d) static
Answer : a
100. Which of these keywords is used to define interfaces in Java?
(a) interface (b) Interface (c) intf (d) Intf
Answer : a
Explanation : None.