Access Modifiers MCQ solution | TCS Fresco Play | Fresco Play | TCS
Make an effort to understand these solutions and apply them to your Hands-On difficulties. (It is not advisable that copy and paste these solutions).
All Question of the MCQs Present Below for Ease Use Ctrl + F with the question name to find the Question. All the Best!
27.Which one of the following modifiers can be applied to a method?
a) transient
b) native
c) volatile
d) friend
Show Answer
Ans: b
28.Given a method in a class, what access modifier do you use to restrict access to that method to only the other members of the same class?
a) static
b) private
c) protected
d) volatile
Show Answer
Ans: b
29.Which of the following modifiers can be applied to a constructor?
a) protected
b) static
c) synchronized
d) transient
Show Answer
Ans: a
30.Which of the following member level (i.e. nonlocal) variable declarations will not compile?
a) transient int b = 3;
b) public static final int c;
c) volatile int d;
d) private synchronized int e;
Show Answer
Ans: b d
31.Which of the following modifiers can be applied to the declaration of a field?
a) abstract
b) volatile
c) native
d) synchronized
Show Answer
Ans: b
32.Which statement is true about the use of modifiers?
a) If no accessibility modifier (public, protected, and private) is specified for a member declaration, the member is only accessible for classes in the same package and subclasses of its class in any package.
b) You cannot specify accessibility of local variables. They are only accessible within the block in which they are declared.
c) Subclasses of a class must reside in the same package as the class they extend.
d) Local variables can be declared static.
Show Answer
Ans: b
33.What is the most restrictive access modifier that will allow members of one class to have access to members of another class in the same package?
a) abstract
b) protected
c) synchronized
d) default access
Show Answer
Ans: d
34.Which statement is true?
a) Constructors can be declared abstract.
b) A subclass of a class with an abstract method must provide an implementation for the abstract method.
c) Transient fields will be saved during serialization.
d) Instance methods of a class can access its static members implicitly.
Show Answer
Ans: d
35.Which statement is true?
a) A static method can call other non-static methods in the same class by using the this keyword.
b) A class may contain both static and non-static variables and both static and non-static methods.
c) Each object of a class has its own instance of each static variable.
d) Instance methods may access local variables of static methods.
Show Answer
Ans: b
36.Which of the following modifiers cannot be applied to a top level class?
a) public
b) private
c) abstract
d) final
Show Answer
Ans: b
37.Which of the following modifiers cannot be applied to a method?
a) final
b) synchronized
c) transient
d) native
Show Answer
Ans: c
38.Which of the following modifiers can be applied to a constructor?
a)private
b) abstract final volatile
Show Answer
Ans: a
39.Which statement is true about modifiers?
a) Fields can be declared native.
b) Non-abstract methods can be declared in abstract classes.
c) Classes can be declared native.
d) Abstract classes can be declared final.
Show Answer
Ans: b
40.Before which of the following can the keyword “synchronized” be placed, without causing a compile error.
a) class variables
b) instance methods
c) instance variables
d) a class
Show Answer
Ans: b
41.A protected method can be overridden by
a) A private method
b) A method without any access specifiers (i.e. default)
c) A protected method
d) All of the above
Show Answer
Ans: c
42.Which statement is true about accessibility of members?
a) Private members are always accessible from within the same package.
b)Private members can only be accessed by code from within the class of the member.
c) A member with default accessibility can be accessed by any subclass of the class in which it is defined.
d) Package/default accessibility for a member can be declared using the keyword default.
Show Answer
Ans: b
43.Which of the following modifiers cannot be applied to the declaration of a field?
a) final
b) transient
c) volatile
d) synchronized
Show Answer
Ans: d
44.How restrictive is the default accessibility compared to public, protected, and private accessibility?
a) Less restrictive than public.
b) More restrictive than public, but less restrictive than protected.
c)More restrictive than protected, but less restrictive than private.
d)More restrictive than private.
Show Answer
Ans: c
45.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. }
a) Nothing. The code fails to compile because the String s isn’t declared correctly.
b) The text “Val = null” is displayed.
c) The text “null” is displayed.
d) Runtime error due to NullPointer exception.
Show Answer
Ans: c
46.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);
}
}
a) The code fails to compile because the variable x is not available to class B.
b) The code compiles correctly, and the following is displayed:012
c) The code fails to compile because you can’t subclass a class with protected variables.
d) The code fails to compile because you can’t subclass a class with static variables.
Show Answer
Ans: a
47.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 {
// …
}
a) By simply referring to the class as MyClass.
b) By simply referring to the class as test.MyClass.
c) By simply referring to the class as com.test.MyClass.
d) By importing with com.* and referring to the class as test.MyClass.
Show Answer
Ans: c
48.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)
Show Answer
Ans: Declarations (1) and (4) cannot occur in the same class definition.
49. Class A is declared in a file named A.java.
package com.test.work;
public class A {
public void m1() {System.out.print(“A.m1, “);}
void m2() {System.out.print(“A.m2, “);}
}
// Class D is declared in a file named D.java.
package com.test.work.other;
import com.test.work.A;
public class D {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
}}
What is the result of attempting to compile and run the program?
a) Prints: A.m1, A.m2,
b) Runtime error occurs.
c) Compile-time error at 1.
d) Compile-time error at 2.
Show Answer
Ans: d
50.public class MyClass {
int calculate(int i, int j)
{
return 2+i*j;
}
public static void main(String [] args) {
int k = MyClass.calculate(5,10);
System.out.println(k);
}
}
What is the result?
a) 70
b) 52
c) Compilation error
d) An exception is thrown at runtime
Show Answer
Ans: c
51.Given the following,
1. package testpkg.p1;
2. public class ParentUtil {
3. public int x = 420;
4. protected int doStuff() { return x; }
5. }
1. package testpkg.p2;
2. import testpkg.p1.ParentUtil;
3. public class ChildUtil extends ParentUtil {
4. public static void main(String [] args) {
5. new ChildUtil().callStuff();
6. }
7. void callStuff() {
8. System.out.print(“this ” + this.doStuff() );
9. ParentUtil p = new ParentUtil();
10. System.out.print(” parent ” + p.doStuff() );
11. }
12. }
Which statement is true?
a) The code compiles and runs, with output this 420 parent 420.
b) If line 8 is removed, the code will compile and run.
c) If line 10 is removed, the code will compile and run.
d) Both lines 8 and 10 must be removed for the code to compile.
Show Answer
Ans: a
52.What would be the result of attempting to compile and run the following program?
.package com.test.work;
public class A {
public void m1() {System.out.print(“A.m1, “);}
protected void m2() {System.out.print(“A.m2, “);}
private void m3() {System.out.print(“A.m3, “);}
void m4() {System.out.print(“A.m4, “);}
}
class B {
public static void main(String[] args) {
A a = new A();
a.m1(); // 1
a.m2(); // 2
a.m3(); // 3
a.m4(); // 4
}}
Assume that the code appears in a single file named A.java.
What is the result of attempting to compile and run the program?
Show Answer
Ans: Compile-time error at 3.
53.Given the following source code, which comment line can be uncommented without introducing errors?
abstract class MyClass {
abstract void f();
final void g() {}
// final void h() {} // (1)
protected static int i;
private int j;
}
final class MyOtherClass extends MyClass {
// MyOtherClass(int n) { m = n; } // (2)
public static void main(String[] args) {
MyClass mc = new MyOtherClass();
}
void f() {}
void h() {}
// void k() { i++; } // (3)
// void l() { j++; } // (4)
int m;
}
Show Answer
Ans: void k() { i++; }
54.Given the following code, which statement can be placed at the indicated position without causing compilation errors?
public class ThisUsage {
int planets;
static int suns;
public void gaze() {
int i;
// … insert statements here …
}
}
a) this = new ThisUsage();
b) this.i = 4;
c) this.planets = i;
d) i = this.planets;
Show Answer
Ans: d
55.What will be the result of compiling the above code ?
class Gamma {
public int display() {
return 3;
}
}
public class Delta extends Gamma {
@Override
protected int display() {
return 4;
}
}
a) The code compiles correctly without any errors.
b) The code fails to compile, because you can’t override a method to be more private than its parent.
c) The code fails to compile, because @Override cannot be mentioned above a protected method.
d) The code fails to compile, because public methods cannot be overriden.
Show Answer
Ans: b
56.What will be the result of compiling the above code ?
class TestAccess {
public int calculate() {
int a=5,b=6;
return a+b;
}
}
public class MyChild extends TestAccess {
@Override
int calculate() {
return 100;
}
}
a) The code fails to compile, because you can’t override a method to be more private than its parent.
b) The code fails to compile, because @Override cannot be mentioned above a default method.
c) The code fails to compile, because public methods cannot be overriden.
d) The code compiles correctly without any errors.
Show Answer
Ans: a
Post a Comment