Java Language Access Control And Declaration MCQs

Java Language Access Control And Declaration MCQs

Try to answer these Java Language Access Control And Declaration MCQs and check your understanding of the Java Language Access Control And Declaration subject. Scroll down and let's begin!

1:

public class A{

static{

System.out.println("static");

}


{

System.out.println("block");

}


public A(){

System.out.println("A");

}


public static void main(String[] args){

A a = new A();

}

}



What is the output for the below code?


A.  

A

B.  

A block static


C.  

static A


D.  

static block A


2:

class Base{

private Base(){

System.out.print("Base");

}

}

public class test extends Base{

public test(){

System.out.print("Derived");

}

public static void main(String[] args){

new test();

}

}



What is the result of compiling and running the following code?


A.  

Exception is thrown at runtime


B.  

Compilation Error


C.  

Derived

D.  

BaseDerived


3:

class Base{

private Base(){

System.out.print("Base");

}

}

public class test extends Base{

public test(){

System.out.print("Derived");

}

public static void main(String[] args){

new test();

}

}



Name the keyword that makes a variable belong to a class, rather than being defined for each instance of the class.


A.  

final

B.  

Native

C.  

static

D.  

abstract

4:

 public class Tester{

static int x = 4;

public Tester(){

System.out.print(this.x); // line 1

Tester();

}

public static void Tester(){ // line 2

System.out.print(this.x); // line 3

}

public static void main(String... args){ // line 4

new Tester();

}

}



What is the result of compiling and running the following code?


A.  

Compile error at line 3 (static methods can't invoke this)


B.  

Compile error at line 4 (invalid argument type for method main )


C.  

Compile error at line 1 (static x must be only accessed inside static methods)


D.  

Compile error at line 2 (constructors can't be static)


5:

public class Tester{

static int x = 4;

public Tester(){

System.out.print(this.x); // line 1

Tester();

}

public static void Tester(){ // line 2

System.out.print(this.x); // line 3

}

public static void main(String... args){ // line 4

new Tester();

}

}



Choose the correct statement. Restriction on static methods are: I. They can only call other static methods.

II. They must only access static data.

III. They cannot refer this or super in any way.


A.  

Only (I)


B.  

Only (III)


C.  

(II) and (III)


D.  

(I), (II) and (III)


6:

static public class Test{

public static void main(String[] args){

char c = 'a';


switch(c){

case 65 : System.out.println("one");break;

case 'a': System.out.println("two");break;

case 3 : System.out.println("three");

}

}

}

What will be the output for the below code?


A.  

Compile error - Illegal modifier for the class Test; only public, abstract & final are permitted


B.  

One

C.  

Two

D.  

Compile error - char can't be permitted in switch statement


7:

static public class Test{

public static void main(String[] args){

char c = 'a';


switch(c){

case 65 : System.out.println("one");break;

case 'a': System.out.println("two");break;

case 3 : System.out.println("three");

}

}

}

A package is a collection of:


A.  

Editing tools


B.  

Classes and Interfaces


C.  

Classes

D.  

Interfaces


8:

static public class Test{

public static void main(String[] args){

char c = 'a';


switch(c){

case 65 : System.out.println("one");break;

case 'a': System.out.println("two");break;

case 3 : System.out.println("three");

}

}

}

A method within a class is only accessible by classes that are defined within the same package as the class of the method. Which one of the following is used to enforce such restriction?


A.  

Declare the method with the keyword private


B.  

Declare the method with the keyword public


C.  

Declare the method with the keyword protected


D.  

Do not declare the method with any accessibility modifiers


9:

public class Test{

public static void main(String args[]){

add();

add(1);

add(1, 2);

}


// insert code here

}

Choose all the lines which if inserted independently instead of "//insert code here" will allow the following code to compile:


A.  

static void add(int[]... args){}


B.  

static void add(int... args, int y){}


C.  

static void add(int args...){}


D.  

static void add(int...args){}


10:

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.  

Runtime error


B.  

It will fail to compile


C.  

Compiles and runs with no output


D.  

Compiles and runs printing

11:

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{}

The object is created with new keyword:


A.  

At run-time


B.  

Depends on the code


C.  

None

D.  

At Compile-time


12:

public class Tester{

static int x = 4;

int y = 9;

public Tester(){

System.out.print(this.x); // line 1

printVariables();

}

public static void printVariables(){

System.out.print(x); // line 2

System.out.print(y); // line 3

}

public static void main(String... args) { // line 4

new Tester();

}

}

What is the result of compiling and running the following code?


A.  

Compile error at line 3 (static methods can't make reference to non-static variables)


B.  

Compile error at line 2 (must access x by writing Tester.x)


C.  

Compile error at line 1 (static x must be only accessed inside static methods)


D.  

Compile error at line 4 (invalid argument type for method main)


13:

public class Test{

public static void main(String args[]){

int x = 10;

x = myMethod(x--);

System.out.print(x);

}


static int myMethod(final int x){

return x--;

}

}

What will be the output after the following program is compiled and executed?


A.  

The program will compile successfully and display 10 as output


B.  

The program will lead to compilation error


C.  

The will compile successfully and display 9 as output


D.  

The program will lead to runtime error


14:

public class Test{

static{

int a = 5;

}


public static void main(String args[]){

new Test().call();

}


void call(){

this.a++;

System.out.print(this.a);

}

}

What will be the output?


A.  

0

B.  

5

C.  

Compile with error


D.  

6