Académique Documents
Professionnel Documents
Culture Documents
A. Test( )
B. Test(void)
C. public Test( )
m
D. public Test(void)
co
SOLUTION :
A and B are wrong because they use the default access modifier and the access
modifier for the class is public (remember, the default constructor has the same access
modifier as the class).
D is wrong. The void makes the compiler think that this is a method specification - in
e.
fact if it were a method specification the compiler would spit it out.
in
Q.2) public class Outer
{
public void someOuterMethod()
{ nl
//Line 5
}
public class Inner { }
tio
public static void main(String[] argv)
{
Outer ot = new Outer();
p
//Line 10
}
}
.a
SOLUTION :
A compiles without problem.
B gives error - non-static variable cannot be referenced from a static context.
C package ot does not exist.
D gives error - non-static variable cannot be referenced from a static context.
Q.3) Which of these keywords cannot be used for a class which has been declared
final?
A. abstract
B. extends
C. Both a & b
D. None of the mentioned
ANSWER : abstract
SOLUTION :
A abstract class is incomplete by itself and relies upon its subclasses to provide
complete implementation. If we declare a class final then no class can inherit that class,
an abstract class needs its subclasses hence both final and abstract cannot be used for
a same class.
Q.4) Which of these is used as default for a member of a class if no access specifier is
Page 1
Questions and Answers
A. private
B. public
C. public, within its own package
D. protected
m
ANSWER : private
SOLUTION :
When we pass an argument by call-by-value a copy of argument is made into the formal
co
parameter of the subroutine and changes made on parameters of subroutine have no
effect on original argument, they remain the same.
e.
public class ArrayTest
{
public static void main(String[ ] args)
in
{
float f1[ ], f2[ ];
f1 = new float[10];
f2 = f1; nl
System.out.println("f2[0] = " + f2[0]);
}
}
tio
A. It prints f2[0] = 0.0
B. It prints f2[0] = NaN
p
to the default values for the primitive data type (float in this case - 0.0), so f1 will contain
10 elements each with a value of 0.0. f2 has been declared but has not been initialised,
it has the ability to reference or point to an array but as yet does not point to any array.
w
are also changes to f2 because both f1 and f2 point to the same array.
Page 2
Questions and Answers
}
}
A. 4, 4
B. 4, 5
C. 5, 4
m
D. Compilation fails.
co
SOLUTION :
D is correct, compilation fails - The return type of getLength( ) in the super class is an
object of reference type Integer and the return type in the sub class is an object of
reference type Long. In other words, it is not an override because of the change in the
return type and it is also not an overload because the argument list has not changed
e.
Q.7) Which of the following statements are incorrect?
in
A. public members of class can be accessed by any code in the program.
B. private members of class can only be accessed by other members of the class.
nl
C. private members of class can be inherited by a sub class, and become protected
members in sub class.
D. protected members of a class can be inherited by a sub class, and become private
tio
members of the sub class.
ANSWER : private members of class can be inherited by a sub class, and become
protected members in sub class.
p
SOLUTION :
private members of a class cannot be inherited by a sub class.
.a
Q.8) What is the stored in the object obj in following lines of code?
box obj;
w
B. NULL
C. Any arbitrary pointer
D. Garbage
w
ANSWER : NULL
SOLUTION :
Memory is allocated to an object using new operator. box obj; just declares a reference
to object, no memory is allocated to it hence it points to NULL.
Q.9) You want subclasses in any package to have access to members of a superclass.
Which is the most restrictive access that accomplishes this objective?
A. public
B. private
C. protected
D. transient
ANSWER : protected
SOLUTION :
Access modifiers dictate which classes, not which instances, may access features.
Methods and variables are collectively known as members. Method and variable
members are given access control in exactly the same way.
private makes a member accessible only from within its own class
protected makes a member accessible only to classes in the same package or subclass
of the class
default access is very similar to protected (make sure you spot the difference) default
Page 3
Questions and Answers
m
transient indicates that a variable is not part of the persistent state of an object.
volatile indicates that a thread must reconcile its working copy of the field with the
master copy every time it accesses the variable.
co
After examining the above it should be obvious that the access modifier that provides
the most restrictions for methods to be accessed from the subclasses of the class from
another package is C - protected. A is also a contender but C is more restrictive, B
would be the answer if the constraint was the "same package" instead of "any package"
in other words the subclasses clause in the question eliminates default.
e.
Q.10) What is the most restrictive access modifier that will allow members of one class
in
to have access to members of another class in the same package?
A. public
B. abstract nl
C. protected
D. default access
tio
ANSWER : default access
SOLUTION :
default access is the "package oriented" access modifier.
A and C are wrong because public and protected are less restrictive. B and D are
p
Q.11) Given a method in a protected class, what access modifier do you use to restrict
access to that method to only the other members of the same class?
w
A. final
B. static
C. private
w
D. protected
ANSWER : protected
w
SOLUTION :
The private access modifier limits access to members of the same class.
A, B, D, and E are wrong because protected are the wrong access modifiers, and final,
static, and volatile are modifiers but not access modifiers.
Q.13) Which of the following class level (nonlocal) variable declarations will not
compile?
A. protected int a;
B. transient int b = 3;
Page 4
Questions and Answers
m
will compile because volatile is a proper variable modifier.
co
class main_class {
public static void main(String args[])
{
int x = 9;
e.
if (x == 9) {
int x = 8;
System.out.println(x);
in
}
}
}
A. 9
nl
B. 8
tio
C. Compilation error
D. Runtime error
SOLUTION :
Two variables with the same name can't be created in a class.
output:
.a
$ javac main_class.java
Exception in thread main java.lang.Error: Unresolved compilation problem:
Duplicate local variable x
w
{
java.util.TreeSet t = new java.util.TreeSet();
t.clear();
}
public void clear()
{
TreeMap m = new TreeMap();
m.clear();
}
}
which two statements, added independently at beginning of the program, allow the code
to compile?
No statement is required
import java.util.*;
import.java.util.Tree*;
import java.util.TreeSet;
import java.util.TreeMap;
A. 1 only
B. 2 and 5
C. 3 and 4
D. 3 and 5
Page 5
Questions and Answers
ANSWER : 2 and 5
SOLUTION :
(2) and (5). TreeMap is the only class that must be imported. TreeSet does not need an
import statement because it is described with a fully qualified name.
(1) is incorrect because TreeMap must be imported. (3) is incorrect syntax for an import
statement. (4) is incorrect because it will not import TreeMap, which is required.
m
Q.16) Which of the following statements is correct?
co
B. Public method is accessible only to subclasses of its parent class
C. Public method can only be called by object of its class.
D. Public method can be accessed by calling object of the public class.
e.
ANSWER : Public method is accessible to all other classes in the hierarchy
SOLUTION :
in
we can access public member of class anywhere in program.
A. malloc
B. alloc
nl
tio
C. new
D. give
ANSWER : new
SOLUTION :
p
Operator new dynamically allocates memory for an object and returns a reference to it.
This reference is address in memory of the object allocated by new.
.a
A. String class
B. Object class
C. Abstract class
w
D. ArrayList class
SOLUTION :
Object class is superclass of every class in Java.
A. Class A
B. Compilation fails.
C. An exception is thrown at line 3.
D. The code executes with no output.
Page 6
Questions and Answers
SOLUTION :
D is correct. The specification at line 3 is for a method and not a constructor and this
method is never called therefore there is no output. The constructor that is called is the
default constructor.
m
{
double getArea(int rad);
}
interface MathPlus
co
{
double getVol(int b, int h);
}
/* Missing Statements ? */
e.
which two code fragments inserted at end of the program, will allow to compile?
class AllMath extends DoMath { double getArea(int r); }
interface AllMath implements MathPlus { double getVol(int x, int y); }
in
interface AllMath extends DoMath { float getAvg(int h, int l); }
class AllMath implements MathPlus { double getArea(int rad); }
abstract class AllMath implements DoMath, MathPlus { public double getArea(int rad) {
return rad * rad * 3.14; } } nl
A. 1 only
tio
B. 2 only
C. 3 and 5
D. 1 and 4
p
ANSWER : 3 and 5
SOLUTION :
(3) are (5) are correct because interfaces and abstract classes do not need to fully
.a
short counter = 0;
void countUp();
}
public class TestCount implements Count
{
public static void main(String [] args)
{
TestCount t = new TestCount();
t.countUp();
}
public void countUp()
{
for (int x = 6; x>counter; x--, ++counter) /* Line 14 */
{
System.out.print(" " + counter);
}
}
}
A. 0 1 2
B. 1 2 3
C. 0 1 2 3
D. Compilation fails
Page 7
Questions and Answers
m
Q.22) Which is a valid declaration within an interface?
co
B. protected short stop = 23;
C. transient short stop = 23;
D. final void madness(short stop);
e.
ANSWER : public static short stop = 23;
SOLUTION :
(A) is valid interface declarations.
in
(B) and (C) are incorrect because interface variables cannot be either protected or
transient. (D) is incorrect because interface methods cannot be final or static.
nl
Q.23) Which of these access specifier must be used for class so that it can be inherited
by another sub class?
tio
A. public
B. private
C. both a & b
D. None of the mentioned
p
ANSWER : public
SOLUTION :
.a
Explanation: only public members can be accessed out of class, private members are
not accessed by external classes. Protected members access only by its sub classes.
w
A. 1 and 3
B. 2 and 4
C. 1 only
D. All of them are legal declarations.
Page 8
Questions and Answers
{
B obj = new B();
obj.j=1;
obj.display();
}
}
m
A. 0
B. 2
co
C. Runtime Error
D. Compilation Error
ANSWER : 2
SOLUTION :
e.
class A is an abstract class, it contains a abstract function display(), the full
implementation of display() method is given in its subclass B, Both the display functions
are the same. Prototype of display() is defined in class A and its implementation is given
in
in class B.
output:
$ javac Abstract_demo.java
$ java Abstract_demo
2
nl
tio
Q.26) You want a class to have access to members of another class in the same
package. Which is the most restrictive access that accomplishes this objective?
A. public
p
B. private
C. protected
D. default access
.a
The only two real contenders are C and D. Protected access C makes a member
accessible only to classes in the same package or subclass of the class. While default
access D makes a member accessible only to classes in the same package.
w
Q.27) Which of these access specifiers must be used for main() method?
w
A. private
B. public
C. protected
D. None of the mentioned
ANSWER : public
SOLUTION :
main() method must be specified public as it called by Java run time system, outside of
the program. If no access specifier is used then by default member is public within its
own package & cannot be accessed by Java run time system.
Page 9
Questions and Answers
obj1.height = 1;
obj1.length = 2;
obj1.width = 1;
obj2 = obj1;
System.out.println(obj2.height);
}
}
m
A. 1
co
B. 2
C. Runtime error
D. Garbage value
ANSWER : 2
e.
SOLUTION :
When we assign an object to another object of same type, all the elements of right side
object gets copied to object on left side of equal to, =, operator.
in
output:
$ javac mainclass.java
$ java mainclass
2 nl
Q.29) What is the output of this program?
tio
class box {
int width;
int height;
int length;
p
}
class mainclass {
public static void main(String args[])
.a
{
box obj = new box();
System.out.println(obj);
w
}
}
w
A. 0
B. 1
w
C. Runtime error
D. Garbage value
A. class
B. struct
C. int
D. None of the mentioned
ANSWER : class
SOLUTION :
Class is keyword used to create user define class definition .
Page 10
Questions and Answers
Q.31) Which two of the following are legal declarations for nonnested classes and
interfaces?
final abstract class Test {}
public static interface Test {}
final public class Test {}
protected abstract class Test {}
protected interface Test {}
m
abstract public class Test {}
co
A. 1 and 4
B. 1 and 4
C. 3 and 6
D. 4 and 6
e.
ANSWER : 3 and 6
SOLUTION :
(3), (6). Both are legal class declarations.
in
(1) is wrong because a class cannot be abstract and
final~Af^Ac~Ac^aEURs^Anot~Ac^a'not^A
nl
Q.32) Which of these method of Object class can clone an object?
A. Objectcopy()
tio
B. copy()
C. Object clone()
D. clone()
p
class Base
{
Base()
w
{
System.out.print("Base");
}
w
}
public class Alpha extends Base
{
public static void main(String[] args)
{
new Alpha(); /* Line 12 */
new Base(); /* Line 13 */
}
}
A. Base
B. BaseBase
C. Compilation fails
D. The code runs with no output
ANSWER : BaseBase
SOLUTION :
B is correct. It would be correct if the code had compiled, and the subclass Alpha had
been saved in its own file. In this case Java supplies an implicit call from the sub-class
constructor to the no-args constructor of the super-class therefore line 12 causes Base
to be output. Line 13 also causes Base to be output.
A is wrong. It would be correct if either the main class or the subclass had not been
instantiated.
Page 11
Questions and Answers
m
public int x = 420;
protected int doStuff() { return x; }
}
co
package testpkg.p2;
import testpkg.p1.ParentUtil;
public class ChildUtil extends ParentUtil
{
e.
public static void main(String [] args)
{
new ChildUtil().callStuff();
in
}
void callStuff()
{
System.out.print("this " + this.doStuff() ); /* Line 18 */
nl
ParentUtil p = new ParentUtil();
System.out.print(" parent " + p.doStuff() ); /* Line 20 */
}
tio
}
which statement is true?
p
A. The code compiles and runs, with output this 420 parent 420.
doStuff() has protected access, and the ChildUtil class is not in the same package as
the ParentUtil class, doStuff() can be accessed only by instances of the ChildUtil class
(a subclass of ParentUtil).
w
Page 12
Questions and Answers
}
}
A. 0
B. 1
C. 2
m
D. Compilation fails.
co
SOLUTION :
A default no-args constructor is not created because there is a constructor supplied that
has an argument, line 4. Therefore the sub-class constructor must explicitly make a call
to the super class constructor:
public Sub(String text)
e.
{
super(text); // this must be the first line constructor
i = 2;
in
}
SOLUTION :
Every class does not need to have a main() method, there can be only one main()
method which is made public.
.a
Q.38) What is the process by which we can control what parts of a program can access
the members of a class?
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Recursion
ANSWER : Encapsulation
SOLUTION :
Encapsulation is one of the pillar of object oriented programming language , it is feature
of class used to bind data and methods into single block and to hide data from external
access.
Page 13
Questions and Answers
m
}
void meth(test o) {
o.a *= 2;
co
O.B /= 2;
}
}
class Output {
public static void main(String args[])
e.
{
test obj = new test(10 , 20);
obj.meth(obj);
in
System.out.println(obj.a + " " + obj.b); }
}
A. 10 20
B. 20 10
nl
tio
C. 20 40
D. 40 20
ANSWER : 20 40
SOLUTION :
p
class objects are always passed by reference, therefore changes done are reflected
back on original arguments. obj.meth(obj) sends object obj as parameter whose
.a
variables a & b are multiplied and divided by 2 respectively by meth() function of class
test. a & b becomes 20 & 40 respectively.
output:
w
$ javac Output.java
$ java Output
20 40
w
A. 0 2 4
B. 0 2 4 6
C. Compilation fails at line 2
Page 14
Questions and Answers
m
Q.41) Which one creates an instance of an array?
co
B. float fa = new float[20];
C. char[ ] ca = "Some String";
D. int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };
e.
ANSWER : int[ ] ia = new int[15];
SOLUTION :
A is correct. It uses correct array declaration and correct array construction.
in
B is incorrect. It generates a compiler error: incompatible types because the array
variable declaration is not correct. The array construction expects a reference type, but
it is supplied with a primitive type in the declaration.
C is incorrect. It generates a compiler error: incompatible types because a string literal
nl
is not assignable to a character type variable.
D is wrong, it generates a compiler error <identifier> expected. The compiler thinks that
you are trying to create two arrays because there are two array initialisers to the right of
tio
the equals, whereas your intention was to create a 3 x 3 two-dimensional array.
class A {
int i;
.a
int j;
A() {
i = 1;
w
j = 2;
}
}
w
class Output {
public static void main(String args[])
{
w
A. false
B. true
C. 1
D. Compilation Error
ANSWER : false
SOLUTION :
obj1 and obj2 are two different objects. equals() is a method of Object class, Since
Object class is superclass of every class it is available to every object.
output:
$ javac Output.java
$ java Output
false
Page 15
Questions and Answers
int i;
int j;
A() {
i = 1;
j = 2;
}
}
m
class Output {
public static void main(String args[])
{
co
A obj1 = new A();
System.out.print(obj1.toString());
}
}
e.
A. true
in
B. false
C. String associated with obj1
D. Compilation Error
A@1cd2e5f
.a
A. 1 and 2
w
B. 2, 3 and 5
C. 3, 4, and 5
D. 2 and 4
ANSWER : 2, 3 and 5
SOLUTION :
(2), (3), and (5). These are all valid interface method signatures.
(1), is incorrect because an interface method must be public; if it is not explicitly
declared public it will be made public implicitly. (4) is incorrect because interface
methods cannot be static.
Q.45) Which of these is used to access member of class before object of that class is
created?
A. public
B. private
C. static
D. protected
ANSWER : static
SOLUTION :
All static member of class are members of class not object to initialize class member we
use static block or static method. To access and initialize member of class we use static
Page 16
Questions and Answers
keyword.
m
{
static int i = 0;
i++;
return i;
co
}
public static void main(String args[])
{
Test test = new Test();
e.
test.aMethod();
int j = test.aMethod();
System.out.println(j);
in
}
}
A. 0
B. 1
nl
C. 2
tio
D. Compilation fails.
Compilation failed because static was an illegal start of expression - method variables
do not have a modifier (they are always considered local).
.a
A. super
B. constant
C. Class
w
D. final
ANSWER : final
w
SOLUTION :
Declaring a class final implicitly declares all of its methods final, and makes the class
inheritable.
Page 17