Sie sind auf Seite 1von 6

1. Which of these access specifiers must be used for main() method?

a) private
b) public
c) protected
d) none of the mentioned
Answer: b

2. Which of these is used to access a member of class before object of that class is created?
a) public
b) private
c) static
d) protected
Answer: c

3. Which of these is used as a default for a member of a class if no access specifier is used for it?
a) private
b) public
c) public, within its own package
d) protected
Answer: a

4. 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: c

5. Which of the following statements are incorrect?


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
c) private members of class can be inherited by a subclass, and become protected members in subclass
d) protected members of a class can be inherited by a subclass, and become private members of the
subclass
Answer: c

6. 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: d
7. Which of these access specifier must be used for class so that it can be inherited by another subclass?
a) public
b) private
c) protected
d) none of the mentioned
Answer: a

8.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?
a) final
b) static
c) private
d) protected
Answer: c

9. Which is a valid declaration within an interface?


a) public static short stop = 23;
b) protected short stop = 23;
c) transient short stop = 23;
d) final void madness(short stop);
Answer: a

10. Which cause a compiler error?


a) int[ ] scores = {3, 5, 7};
b) int [ ][ ] scores = {2,7,6}, {9,3,45};
c) String cats[ ] = {"Fluffy", "Spot", "Zeus"};
d) boolean results[ ] = new boolean [] {true, false, true};
Answer:b

11. 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
b) Private
c) Protected
d) Default access
Answer:d

12. Which one creates an instance of an array?

a) int[ ] ia = new int[15];

b) float fa = new float[20];

c) char[ ] ca = "Some String";

d) int ia[ ] [ ] = { 4, 5, 6 }, { 1,2,3 };


Answer:a
13. 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:c

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

public class Outer


{
public void someOuterMethod()
{
//Line 5
}
public class Inner { }

public static void main(String[] argv)


{
Outer ot = new Outer();
//Line 10
}
}
a)new Inner(); //At line 5
b)new Inner(); //At line 10
c)new ot.Inner(); //At line 10
d)new Outer.Inner(); //At line 10

Answer:a

15. 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:d

16. What will be the output of the program?


public class Test
{
public int aMethod()
{
static int i = 0;
i++;
return i;
}
public static void main(String args[])
{
Test test = new Test();
test.aMethod();
int j = test.aMethod();
System.out.println(j);
}
}

a) 0

b) 1

c) 2

d) Compilation fails.
Answer:d

17.Which of the following is/are legal method declarations?


1. protected abstract void m1();
2. static final void m1(){}
3. synchronized public final void m1() {}
4. private native void m1();

a) 1 and 3

b) 2 and 4
c) 1 only

d) All of them are legal declarations.


Answer:d

18. Which of the following class level (nonlocal) variable declarations will not compile?

a) protected int a;

b) transient int b = 3;

c) private synchronized int e;

d) volatile int d;
Answer:c

19. Choose the correct statement


public class Circle{
private double radius;
public Circle(double radius)
{
radius = radius;
}
}
a) compilation error because it does not have a main method.
b)The program will compile, but we cannot create an object of Circle with a specified radius. The object will
always have radius 0.
c)The program has a compilation error because we cannot assign radius to radius.
d)The program does not compile because Circle does not have a default constructor.
Answer:b

20. You have the following code in a file called Test.java


class Base{
public static void main(String[] args){
System.out.println("Hello");
}
}
public class Test extends Base{}
What will happen if you try to compile and run this?
a) It will fail to compile.
b) Runtime error
c) Compiles and runs with no output.
d) Compiles and runs printing
Answer:d

Das könnte Ihnen auch gefallen