Declaration And Access Control

Name: _____________________

Date: _____________________

Instructions: Answer all questions. Write your answers clearly in the space provided.

Question 1:

What is the default access modifier for class members in Java if no access modifier is specified?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 2:

Which access modifier restricts a class member's visibility only to the same class and its subclasses?

A. private
B. public
C. protected
D. package-private
Answer: _________
Question 3:

What keyword is used to declare a constant in Java, which is implicitly final and static?

A. final
B. const
C. static
D. constant
Answer: _________
Question 4:

Which of the following access modifiers allows access to a class member from any class in any package?

A. protected
B. private
C. public
D. default
Answer: _________
Question 5:

What is the access level of a class member with no access modifier specified in Java?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 6:

In Java, which access modifier allows a method or variable to be accessed within the same package only?

A. private
B. public
C. protected
D. package-private
Answer: _________
Question 7:

Which keyword is used to explicitly specify that a method cannot be overridden in a subclass?

A. override
B. final
C. static
D. super
Answer: _________
Question 8:

What is the default access modifier for a class in Java if no access modifier is specified?

A. private
B. public
C. protected
D. package-private
Answer: _________
Question 9:

Which access modifier allows a class member to be accessed only within its own class and package?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 10:

In Java, which access modifier allows a class member to be accessed from anywhere in the code?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 11:

Suppose a class has public visibility. In this class we define a protected method. Which of the following statements is correct?

A. This method is only accessible from inside the class itself and from inside all subclasses.
B. In a class, you cannot declare methods with a lower visibility than the visibility of the class in which it is defined.
C. From within protected methods you do not have access to public methods.
D. This method is accessible from within the class itself and from within all classes defined in the same package as the class itself.
Answer: _________
Question 12:

The object is created with new keyword

A. At Compile-time
B. At run-time
C. Depends on the code
D. None of these
Answer: _________
Question 13:

A package is a collection of

A. Classes
B. Interfaces
C. Editing tools
D. Classes and Interfaces
Answer: _________
Question 14:

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 public.
B. Declare the method with the keyword private.
C. Declare the method with the keyword protected.
D. Do not declare the method with any accessibility modifiers.
Answer: _________
Question 15:

Choose the correct statement. Restriction on static methods are: I.xa0xa0xa0They can only call other static methods. II.xa0xa0 They must only access static data. III.xa0They cannot refer this or super in any way.

A. Only (I)
B. (I) and (II)
C. (II) and (III)
D. Only (III)
Answer: _________
Question 16:

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

A. static
B. final
C. abstract
D. native
Answer: _________
Question 17:

What keyword is used to declare a method that can be called without creating an instance of the class?

A. static
B. final
C. public
D. this
Answer: _________
Question 18:

Which of the following access modifiers restricts a method or variable's access to the same package only?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 19:

In Java, what is the purpose of the static keyword when applied to a variable?

A. It makes the variable non-static.
B. It initializes the variable.
C. It allows access from any class.
D. It makes the variable constant.
Answer: _________
Question 20:

Which keyword is used to define a superclass's method that is overridden by a subclass method?

A. override
B. extends
C. super
D. implement
Answer: _________
Question 21:

What is the access level of a class member declared as private in Java?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 22:

Which access modifier allows a class member to be accessed within the same package and its subclasses?

A. private
B. public
C. protected
D. package-private
Answer: _________
Question 23:

What is the keyword used to declare a class that cannot be instantiated and can only be extended?

A. final
B. const
C. static
D. abstract
Answer: _________
Question 24:

In Java, what access modifier is used to specify that a class member can be accessed only within its class?

A. protected
B. private
C. public
D. package-private
Answer: _________
Question 25:

Which of the following is not an access modifier in Java?

A. abstract
B. final
C. transient
D. static
Answer: _________
Question 26:

What access modifier allows a class member to be accessed from any class in the same package or any subclass?

A. protected
B. private
C. public
D. default
Answer: _________
Question 27:

In Java, which access modifier allows a class member to be accessed from any class, even outside the package?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 28:

What keyword is used to declare a method that is implemented in a subclass to provide a specific implementation?

A. abstract
B. override
C. implement
D. extends
Answer: _________
Question 29:

In Java, what is the purpose of the final keyword when applied to a method?

A. It makes the method non-final.
B. It allows method overriding.
C. It makes the method constant.
D. It makes the method static.
Answer: _________
Question 30:

Which access modifier allows a class member to be accessed from any class in the same package but not from outside?

A. protected
B. private
C. public
D. default
Answer: _________
Question 31:

What keyword is used to declare a variable that can be accessed without creating an instance of the class?

A. static
B. final
C. public
D. this
Answer: _________
Question 32:

Which access modifier restricts a class member's visibility to only the same class, its package, and subclasses?

A. default
B. private
C. public
D. protected
Answer: _________
Question 33:

In Java, what is the purpose of the final keyword when applied to a class?

A. It makes the class non-final.
B. It prevents subclassing.
C. It makes the class constant.
D. It makes the class abstract.
Answer: _________
Question 34:

Which keyword is used to declare a method that cannot be overridden in a subclass?

A. final
B. static
C. super
D. this
Answer: _________
Question 35:

What is the access level of a class member declared as protected in Java?

A. public
B. protected
C. private
D. package-private
Answer: _________
Question 36:

Which keyword is used to explicitly specify that a method should be overridden in a subclass?

A. override
B. extends
C. super
D. implement
Answer: _________
Question 37:

Choose all the lines which if inserted independently instead of "//insert code here" will allow the following code to compile: public class Test{
public static void main(String args[]){
add()
add(1)
add(1, 2)
}
// insert code here
}

A. void add(Integer... args){}
B. static void add(int... args, int y){}
C. static void add(int args...){}
D. static void add(int[]... args){}
Answer: _________
Question 38:

What is the result of compiling and running the following code? 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()
}
}

A. BaseDerived
B. Derived
C. Exception is thrown at runtime
D. Compilation Error
Answer: _________
Question 39:

What is the result of compiling and running the following code? 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()
}
}

A. Compile error at line 1 (static x must be only accessed inside static methods)
B. Compile error at line 2 (constructors can't be static)
C. Compile error at line 3 (static methods can't invoke this)
D. Compile error at line 4 (invalid argument type for method main )
Answer: _________
Question 40:

What is the result of compiling and running the following code? 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()
}
}

A. Compile error at line 1 (static x must be only accessed inside static methods)
B. Compile error at line 3 (static methods can't make reference to non-static variables)
C. Compile error at line 4 (invalid argument type for method main)
D. 49
Answer: _________
Question 41:

Consider the following two classes declared and defined in two different packages, what can be added in class B to form what considered a correct access to class A from main() method of class B? package subPackage
public class A { }
package anotherPackage
// line 1
public class B{
public static void main(String[] args){
// line 2
}
} 1. At line1 add noting
At line2 add: new A()
2. At line 1 add: import package.*
at line 2 add : new subPackage.A()
3. At line 1 add: import subPackage.*
at line 2 add : new A()
4. At line 1 add: import subPackage.A
at line 2 add : new A()

A. 1 and 2
B. 2 and 4
C. 3 and 4
D. 1 and 3
Answer: _________
Question 42:

Determine output: public class InitDemo{
static int i = demo()
static{
System.out.print(i)
}
InitDemo(){
System.out.print("hello1")
}
public static void main(String... args){
System.out.print("Hello2")
}
static int demo(){
System.out.print("InsideDemo")
return 10
}
}

A. Compilation error.
B. IllegalArgumentException is thrown at runtime.
C. InsideDemo 10 Hello2
D. Hello2 InsideDemo 10
Answer: _________
Question 43:

Which statements are most accurate regarding the following classes? class A{
private int i
protected int j
}
class B extends A{
private int k
protected int m
}

A. An object of B contains data fields i, j, k, m.
B. An object of B contains data fields j, k, m.
C. An object of B contains data fields j, m.
D. An object of B contains data fields k, m.
Answer: _________
Question 44:

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

A. The program has 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: _________
Question 45:

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: _________
Question 46:

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

A. Compile with error
B. Runtime Exception
C. 5
D. 6
Answer: _________
Question 47:

Determine Output: class MyClass{
static final int a = 20
static final void call(){
System.out.println("two")
}

static{
System.out.println("one")
}
}
public class Test{
public static void main(String args[]){
System.out.println(MyClass.a)
}
}

A. one
B. one two
C. one two 20
D. 20
Answer: _________
Question 48:

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()
}
}

A. A block static
B. static block A
C. static A
D. A
Answer: _________
Question 49:

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

A. abc
B. xyz
C. Compilation fails
D. Compilation clean but no output
Answer: _________
Question 50:

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

A. Compile with error
B. 5
C. 0
D. Runtime Exception
Answer: _________
Question 51:

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 ")
}
}

A. r1 r4 pre b1 b2 post
B. pre r1 r4 b1 b2 r2 r3 post
C. r1 r4 pre b1 b2 r3 r2 post
D. r1 r4 pre b1 b2 post r3 r2
Answer: _________
Question 52:

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")
}
}
}

A. one
B. two
C. Compile error - char can't be permitted in switch statement.
D. Compile error - Illegal modifier for the class Test only public, abstract & final are permitted.
Answer: _________
Question 53:

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

A. one two 1
B. one two 10
C. one two 0
D. Compile time error.
Answer: _________
Question 54:

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

A. The will compile successfully and display 9 as output.
B. The program will lead to compilation error.
C. The program will lead to runtime error.
D. The program will compile successfully and display 10 as output.
Answer: _________
Question 55:

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

A. Only the Test class.
B. Any class.
C. Any class in com.mypackage package.
D. Any class that extends Test.
Answer: _________

Answer Key

1: D
2: C
3: A
4: C
5: D
6: D
7: B
8: D
9: D
10: A
11: D
12: B
13: D
Solution: Think of a package like a folder or a directory on your computer. What do you usually put in a folder? Files! In Java, packages help us organize our code. Classes and Interfaces are both types of "files" in Java. So, a package can contain both. Option A is partially correct (Classes), but it's not the whole story. Option B is also partially correct (Interfaces), but it's also not the complete picture. Option C and Option E are incorrect because "Editing tools" are not stored within the Java package. Option D is the most accurate. Therefore, the correct answer is D: Classes and Interfaces.
14: D
15: N/A
16: A
17: A
18: D
19: C
20: C
21: C
22: C
23: D
24: B
25: A
26: C
27: A
28: A
29: C
30: D
31: A
32: D
33: B
34: A
35: B
36: A
37: N/A
Solution: var-args = variable number of arguments = 0 or many void add(Integer... args){} is correct IF made "static" as it's called from a static context: main(). Var-args can be of both object(eg Integer) and primitive(eg int) types. static void add(int... args, int y){} is correct IF its parameters' order is reversed. If a method has both var-arg(0 or MAX 1) + non-var-args(0 or more) parameters then the var-arg parameter MUST come LAST! static void add(int args...){} : "..." must come after the type of the var-arg parameter, not after its name static void add(int[]... args){} : for this to be a correct declaration then add() should have been called something like this: "add(arr)
" or "add(arr, arr)
" where "arr" could be defined as "int[] arr = new int[5]
" static void add(int...args){} is a valid way to define var-args (there is no need to have any space between "..." and the type and name of the var-arg param)
38: D
Solution: Implicit super constructor Base is not visible, must explicitly invoke another constructor.
39: C
Solution: static methods can't invoke 'this'.
40: B
Solution: static methods cannot make references to non-static variables.
41: C
42: C
Solution: As soon as the class are loaded static variables are initialized first. To initialize it demo must be called first then then static block executes and then main method is called.
43: B
44: B
45: D
Solution: This will compile and print "Hello". The entry point for a standalone java program is the main method of the class that is being run. The java run-time system will look for that method in class Test and find that it should have such a method. It does not matter whether it is defined in the class itself or is inherited from a parent class.
46: A
47: N/A
48: B
Solution: First execute static block, then statement block and then constructor.
49: A
Solution: Java pass reference as value. passing the object reference, and not the actual object itself. Simply reassigning to the parameter used to pass the value into the method will do nothing, because the parameter is essentially a local variable.
50: A
Solution: A variable declared in a static initializer is not accessible outside its enclosing block.
51: C
Solution: All static blocks execute first then blocks and constructor. Blocks and constructor executes (super class block then super class constructor, sub class block then sub class constructor). Sequence for static blocks is super class first then sub class. Sequence for blocks is super class first then sub class.
52: D
Solution: Outer class can only declare public , abstract and final. Illegal modifier for the class Test
only public, abstract & final are permitted.
53: D
Solution: Static members are common for all objects, where as ‘this’ refer to a particular object. so we cant use ‘this’ operator in the static methods.
54: B
Solution: While compiling the Test class, the compilation error occurs implying that the final parameter x cannot be assigned a value. Therefore the option B is correct and remaining options are incorrect.
55: A