Vous êtes sur la page 1sur 17

Questions and Answers

Q.1) public class Test { }


What is the prototype of the default constructor?

A. Test( )
B. Test(void)
C. public Test( )

m
D. public Test(void)

ANSWER : public Test( )

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

Which of the following code fragments inserted, will allow to compile?


w

A. new Inner(); //At line 5


B. new Inner(); //At line 10
C. new ot.Inner(); //At line 10
w

D. new Outer.Inner(); //At line 10

ANSWER : new Inner(); //At line 5


w

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

used for it?

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.

Q.5) What will be the output of the program?

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

C. An error at f2 = f1; causes compile to fail.


D. It prints the garbage value.
.a

ANSWER : It prints f2[0] = 0.0


SOLUTION :
A is correct. When you create an array (f1 = new float[10];) the elements are initialises
w

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

f2 = f1; copies the reference (pointer/memory address) of f1 into f2 so now f2 points at


the array pointed to by f1.
This means that the values returned by f2 are the values returned by f1. Changes to f1
w

are also changes to f2 because both f1 and f2 point to the same array.

Q.6) What will be the output of the program?


class Super
{
public Integer getLength()
{
return new Integer(4);
}
}
public class Sub extends Super
{
public Long getLength()
{
return new Long(5);
}

public static void main(String[] args)


{
Super sooper = new Super();
Sub sub = new Sub();
System.out.println(
sooper.getLength().toString() + "," + sub.getLength().toString() );

Page 2
Questions and Answers

}
}

A. 4, 4
B. 4, 5
C. 5, 4

m
D. Compilation fails.

ANSWER : 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

A. Memory address of allocated memory of object.


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

access makes a member accessible only to classes in the same package.


public means that all other classes regardless of the package that they belong to, can
access the member (assuming the class itself is visible)
final makes it impossible to extend a class, when applied to a method it prevents a
method from being overridden in a subclass, when applied to a variable it makes it
impossible to reinitialise a variable once it has been initialised
abstract declares a method that has not been implemented.

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

wrong because abstract and synchronized are not access modifiers.


.a

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.12) Which of the following is a valid declaration of an object of class Box?

A. Box obj = new Box();


B. Box obj = new Box;
C. obj = new Box();
D. new Box obj;

ANSWER : Box obj = new Box();


SOLUTION :
new is keyword used to allocate dynamic memory.
Box obj = new Box();
This is syntax to create object in java.

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

C. private synchronized int e;


D. volatile int d;

ANSWER : private synchronized int e;


SOLUTION :
C will not compile; the synchronized modifier applies only to methods.
A and B will compile because protected and transient are legal variable modifiers. D

m
will compile because volatile is a proper variable modifier.

Q.14) What is the output of this program?

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

ANSWER : Compilation error


p

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

Q.15) /* Missing statements ? */


w

public class NewTreeSet extends java.util.TreeSet


{
public static void main(String [] args)
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?

A. Public method is accessible to all other classes in the hierarchy

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.

Q.17) Which of these operators is used to allocate memory for an object?

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

Q.18) Which of these class is superclass of every class in Java?


w

A. String class
B. Object class
C. Abstract class
w

D. ArrayList class

ANSWER : Object class


w

SOLUTION :
Object class is superclass of every class in Java.

Q.19) What will be the output of the program?


public class A
{
void A() /* Line 3 */
{
System.out.println("Class A");
}
public static void main(String[] args)
{
new A();
}
}

A. Class A
B. Compilation fails.
C. An exception is thrown at line 3.
D. The code executes with no output.

ANSWER : 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.

Q.20) interface DoMath

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

implement the interfaces they extend or implement (respectively).


(1) is incorrect because a class cannot extend an interface. (2) is incorrect because an
interface cannot implement anything. (4) is incorrect because the method being
w

implemented is from the wrong interface.


w

Q.21) What will be the output of the program?


interface Count
{
w

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

ANSWER : Compilation fails


SOLUTION :
The code will not compile because the variable counter is an interface variable that is by
default final static. The compiler will complain at line 14 when the code attempts to
increment counter.

m
Q.22) Which is a valid declaration within an interface?

A. public static short stop = 23;

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

Q.24) Which of the following is/are legal method declarations?


protected abstract void m1();
static final void m1(){}
w

synchronized public final void m1() {}


private native void m1();
w

A. 1 and 3
B. 2 and 4
C. 1 only
D. All of them are legal declarations.

ANSWER : All of them are legal declarations.


SOLUTION :
All the given statements are legal declarations.

Q.25) What is the output of this program?


abstract class A {
int i;
abstarct void display();
}
class B extends A {
int j;
void display() {
System.out.println(j);
}
}
class Abstract_demo {
public static void main(String args[])

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

ANSWER : default access


SOLUTION :
w

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.

Q.28) What is the output of this program?


class box {
int width;
int height;
int length;
}
class mainclass {
public static void main(String args[])
{
box obj1 = new box();
box obj2 = new box();

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

ANSWER : Garbage value


SOLUTION :
Object obj of box class contains reference to the memory which was given to its class
instances. Printing obj will print the address of the memory.
output:
$ javac mainclass.java
$ java mainclass
box@130671e

Q.30) Which of these keywords is used to make a class?

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

ANSWER : Object clone()


SOLUTION :
To make same copy of object use clone method of object class
.a

Q.33) What will be the output of the program?


w

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

C is wrong. The code compiles.


D is wrong. There is output.

Q.34) package testpkg.p1;


public class ParentUtil
{

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.

B. If line 18 is removed, the code will compile and run.


.a

C. If line 20 is removed, the code will compile and run.


D. An exception is thrown at runtime.
w

ANSWER : If line 20 is removed, the code will compile and run.


SOLUTION :
The ParentUtil instance p cannot be used to access the doStuff() method. Because
w

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

A, B, D, and E are incorrect because of the access rules described previously.

Q.35) What will be the output of the program?


class Super
{
public int i = 0;

public Super(String text) /* Line 4 */


{
i = 1;
}
}
class Sub extends Super
{
public Sub(String text)
{
i = 2;
}
public static void main(String args[])
{
Sub sub = new Sub("Hello");
System.out.println(sub.i);

Page 12
Questions and Answers

}
}

A. 0
B. 1
C. 2

m
D. Compilation fails.

ANSWER : 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
}

Q.36) Which of these statement is incorrect?


nl
A. Every class must contain a main() method.
B. Applets do not require a main() method at all.
tio
C. There can be only one main() method in a program.
D. main() method must be made public.

ANSWER : Every class must contain a main() method.


p

SOLUTION :
Every class does not need to have a main() method, there can be only one main()
method which is made public.
.a

Q.37) Which cause a compiler error?


w

A. int[ ] scores = {3, 5, 7};


B. int [ ][ ] scores = {2,7,6}, {9,3,45};
w

C. String cats[ ] = {"Fluffy", "Spot", "Zeus"};


D. boolean results[ ] = new boolean [] {true, false, true};
w

ANSWER : int [ ][ ] scores = {2,7,6}, {9,3,45};


SOLUTION :
B generates a compiler error: <identifier> expected. The compiler thinks you are trying
to create two arrays because there are two array initialisers to the right of the equals,
whereas your intention was to create one 3 x 3 two-dimensional array.
To correct the problem and make B compile you need to add an extra pair of curly
brackets:
int [ ] [ ] scores = { {2,7,6}, {9,3,45} };

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

Q.39) What is the output of this program?


class test {
int a;
int b;
test(int i, int j) {
a = i;
b = j;

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

Q.40) What will be the output of the program?


import java.util.*;
w

public class NewTreeSet2 extends NewTreeSet


{
public static void main(String [] args)
{
NewTreeSet2 t = new NewTreeSet2();
t.count();
}
}
protected class NewTreeSet
{
void count()
{
for (int x = 0; x < 7; x++,x++ )
{
System.out.print(" " + x);
}
}
}

A. 0 2 4
B. 0 2 4 6
C. Compilation fails at line 2

Page 14
Questions and Answers

D. Compilation fails at line 10

ANSWER : Compilation fails at line 10


SOLUTION :
Nonnested classes cannot be marked protected (or final for that matter), so the compiler
will fail at protected class NewTreeSet.

m
Q.41) Which one creates an instance of an array?

A. int[ ] ia = new int[15];

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.

Q.42) What is the output of this program?


p

class A {
int i;
.a

int j;
A() {
i = 1;
w

j = 2;
}
}
w

class Output {
public static void main(String args[])
{
w

A obj1 = new A();


A obj2= new A();
System.out.print(obj1.equals(obj));
}
}

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

Q.43) What is the output of this program?


class A {

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

ANSWER : String associated with obj1


SOLUTION :
nl
tio
toString() is method of class Object, since it is superclass of every class, every object
has this method. toString() returns the string associated with the calling object.
output:
$ javac Output.java
$ java Output
p

A@1cd2e5f
.a

Q.44) Which three are valid method signatures in an interface?


private int getArea();
public float getVol(float x);
w

public void main(String [] args);


public static void main(String [] args);
boolean setFlag(Boolean [] test);
w

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.

Q.46) What will be the output of the program?


public class Test
{
public int aMethod()

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.

ANSWER : Compilation fails.


SOLUTION :
p

Compilation failed because static was an illegal start of expression - method variables
do not have a modifier (they are always considered local).
.a

Q.47) Which of these keywords can be used to prevent inheritance of a class?


w

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

Vous aimerez peut-être aussi