Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 2

What is printed out following the execution of the code below ?

1. class Test {
2.

static String s;

3.

public static void main(String []args) {

4.

int x = 4;

5.

if (x < 4)

6.

System.out.println("Val = " + x);

7.

else

8.

System.out.println(s);

9.

10. }
Nothing. The code fails to compile because the String s isnt declared correctly.
The text "Val = null" is displayed.
The text "null" is displayed.
Runtime error due to NullPointer exception.
Topic: Access Modifiers
Analyse the following 2 classes and select the correct statement.
class A {
private int x = 0;
static int y = 1;
protected int q = 2;
}
class B extends A {
void method() {
System.out.println(x);
System.out.println(y);
System.out.println(q);
}

}
The code fails to compile because the variable x is not available to class B.
The code compiles correctly, and the following is displayed: 0 1 2
The code fails to compile because you cant subclass a class with protected
variables.
The code fails to compile because you cant subclass a class with static variables.
Topic: Access Modifiers
Given the following class, which of these is valid way of referring to the
class from outside of the package com.test?
package com.test;
public class MyClass {
// ...
}
By simply referring to the class as MyClass.
By simply referring to the class as test.MyClass.
By simply referring to the class as com.test.MyClass.
By importing with com.* and referring to the class as test.MyClass.
Topic: Access Modifiers
Given the following member declarations, which statement is true?
int a;

// (1)

static int a;

// (2)

int f() { return a; }

// (3)

static int f() { return a; } // (4)


Declarations (1) and (3) cannot occur in the same class definition.
Declarations (2) and (4) cannot occur in the same class definition.
Declarations (1) and (4) cannot occur in the same class definition.
Declarations (2) and (3) cannot occur in the same class definition.

You might also like