Computer

Declaration And Access Control MCQs

Practice Declaration And Access Control MCQs for competitive exams.

Declaration And Access Control MCQs

Practice questions from this topic.

What can directly access and change the value of the variable qusNo? package com.mypackage; public class Test{ private int qusNo = 100; }

  1. A. Only the Test class.
  2. B. Any class.
  3. C. Any class in com.mypackage package.
  4. D. Any class that extends Test. E. None of these
Report Error

What will be the output after the following program is compiled and executed? 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--; } }

  1. A. The will compile successfully and display 9 as output.
  2. B. The program will lead to compilation error.
  3. C. The program will lead to runtime error.
  4. D. The program will compile successfully and display 10 as output. E. None of these
Report Error

What will be the output after compiling and running following program code? public class Test{ static int a; public static void main(String[] args){ System.out.println("one"); call(1); } static void call(int a){ this.a=10; System.out.println("two "+a); } }

  1. A. one two 1
  2. B. one two 10
  3. C. one two 0
  4. D. Compile time error. E. None of these
Report Error

What will be the output for the below code? 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"); } } }

  1. A. one
  2. B. two
  3. C. Compile error - char can't be permitted in switch statement.
  4. D. Compile error - Illegal modifier for the class Test; only public, abstract & final are permitted. E. None of these
Report Error

Determine output: class A{ { System.out.print("b1 "); } public A(){ System.out.print("b2 "); } } class B extends A{ static{ System.out.print("r1 "); } public B(){ System.out.print("r2 "); } { System.out.print("r3 "); } static{ System.out.print("r4 "); } } public class Test extends B{ public static void main(String[] args){ System.out.print("pre "); new Test(); System.out.println("post "); } }

  1. A. r1 r4 pre b1 b2 post
  2. B. pre r1 r4 b1 b2 r2 r3 post
  3. C. r1 r4 pre b1 b2 r3 r2 post
  4. D. r1 r4 pre b1 b2 post r3 r2 E. Compilation fail
Report Error

What will be the output for the below code? public class Test{ static{ int a = 5; } public static void main(String[] args){ System.out.println(a); } }

  1. A. Compile with error
  2. B. 5
  3. C. 0
  4. D. Runtime Exception E. None of these
Report Error

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

  1. A. static
  2. B. final
  3. C. abstract
  4. D. native E. volatile
Report Error

What will be the output? public class Test{ public static void main(String[] args){ String value = "abc"; changeValue(value); System.out.println(value); } public static void changeValue(String a){ a = "xyz"; } }

  1. A. abc
  2. B. xyz
  3. C. Compilation fails
  4. D. Compilation clean but no output E. None of these
Report Error

What is the output for the below code? 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(); } }

  1. A. A block static
  2. B. static block A
  3. C. static A
  4. D. A E. None of these
Report Error

What will be the output? 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); } }

  1. A. Compile with error
  2. B. Runtime Exception
  3. C. 5
  4. D. 6 E. 0
Report Error

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?

  1. A. It will fail to compile.
  2. B. Runtime error
  3. C. Compiles and runs with no output.
  4. D. Compiles and runs printing
Report Error

Choose the correct statement public class Circle{ private double radius; public Circle(double radius){ radius = radius; } }

  1. A. The program has a compilation error because it does not have a main method.
  2. B. The program will compile, but we cannot create an object of Circle with a specified radius. The object will always have radius 0.
  3. C. The program has a compilation error because we cannot assign radius to radius.
  4. D. The program does not compile because Circle does not have a default constructor.
Report Error