MCQs > IT & Programming > Java MCQs > Java Language Access Control And Declaration MCQs

Java Language Access Control And Declaration MCQ

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?


Answer

Correct Answer:

Compile with error



Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer:

The program will lead to compilation error



Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer:

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



Note: This Question is unanswered, help us to find answer for this one

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:


Answer

Correct Answer:

At run-time



Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer:

Compiles and runs printing


Note: This Question is unanswered, help us to find answer for this one

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:


Answer

Correct Answer:

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



Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer:

Do not declare the method with any accessibility modifiers



Note: This Question is unanswered, help us to find answer for this one

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:


Answer

Correct Answer:

Classes and Interfaces



Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer:

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



Note: This Question is unanswered, help us to find answer for this one

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.


Answer

Correct Answer:

(I), (II) and (III)



Note: This Question is unanswered, help us to find answer for this one

 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?


Answer

Correct Answer:

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



Note: This Question is unanswered, help us to find answer for this one

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.


Answer

Correct Answer:

static


Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer:

Compilation Error



Note: This Question is unanswered, help us to find answer for this one

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?


Answer

Correct Answer:

static block A



Note: This Question is unanswered, help us to find answer for this one