Access Modifiers 1.2
Access Modifiers 1.2
2 class num1{
3 public int a;
4 protected int b;
5 void data(int c, int d) {
6 a = c;
7 b = d;
8 System.out.println(a + " " + b);
9 }
10 }
11 class num2 extends num1{
12 public int mul;
13 void multiplication() {
14 mul = a * b;
15 }
16 }public class Main{
17 public static void main(String args[]) {
18 num2 obj = new num2();
19 obj.data(20, 30);
20 obj.multiplication();
21 }
22 }
Question 1
A) 20 30
B) 60
C) 600
D) Compilation error
1 //Predict the Output
2 public class Main{
3 public static void main(String s[]){
4 Noodles eat = new Noodles();
5 eat.print();
6 }
7 }
8 class Maggi{
9 private int spoon = 3;
10 protected int sticks = 4;
11 }
12 class Noodles extends Maggi{
13 private int powder = 1;
14 protected int fire = 2;
15 void print()
16 {
17 System.out.println(powder + " " + fire + " " + sticks + " " +
18 spoon);
19 }
20 }
21
22
Question 2
A) 1 2 4 3
B) 4 2 3 1
C) 1
D) Compilation error
1 //Predict the Output
2 public class Main{
3 public static void main(String s[]) {
4 Things t = new Things();t.print();
5 product p = new product();p.print();
6 System.out.println("~" + p.Thg2);
7 }
8 }
9 class product{
10 private int Thg1 = 3;
11 protected int Thg2 = 4;
12 void print() {
13 System.out.print("~" + Thg1);
14 }
15 }
16 class Things extends product{
17 private int Thg3 = 1;
18 protected int Thg4 = 2;
19 void print() {
20 System.out.print(Thg3 + "~" + Thg4 + "~" + Thg2);
21 }
22 }
Question 3
A) 1~2~4~3
B) 1~2~4~3~4
C) 3
D) Compilation error
1 //Predict the Output
2 interface SixesMachine {
3 void hitSixes();
4 }
5 public abstract class DhoniInTheMaking implements SixesMachine {
6 public String numberOfSixes() {
7 return "6 0 6 3 6 6 6 6";
8 }
9 private void printRunsTrail(String runsAndRuns){
10 System.out.println(runsAndRuns);
11 }
12 }
13 class Dhoni extends DhoniInTheMaking {
14 public static void main(String args[]) {
15 DhoniInTheMaking outputClass = new Dhoni();
16 outputClass.hitSixes();
17 }
18 public void hitSixes() {
19 numberOfSixes().substring(5, 12);
20 printRunsTrail(numberOfSixes().substring(3, 7));
21 }
22 }
Question 4
A) 6 0 6 3 6 6 6 6
B) 3 7
C) 5 12
D) Compilation error
1 //Predict the Output
2 public class Main
3 {
4 public static void main(String[] args)
5 {
6 Guess g = new Guess();
7 g.G();
8 Guess.CantGuess.ICanGuess.Hai hai = null;
9 hai.print();
10 }
11 }
12 class Guess {
13 protected void G() {
14 CantGuess cg = new CantGuess();
15 }
16 class CantGuess
17 {
18 public CantGuess()
19 {
20 System.out.print("Vowel");
21 }
22
1 ICanGuess icg = new ICanGuess();
2 GetICanGuess gicg = new GetICanGuess();
3 class ICanGuess {
4 public ICanGuess() {
5 System.out.print("Words");
6 }
7 Hai h = new Hai();
8 public class Hai {
9 public Hai() {
10 System.out.print("Check");
11 }
12 public void print() {
13 System.out.print("print");
14 }
15 }
16 }
17 class GetICanGuess {
18 public GetICanGuess(){
19 System.out.print("String");}
20 }
21 }
22 }
Question 5
A) CheckWordsStringVowel prints the infinite time
B) CheckWordsStringVowel
D) Compilation error
1 //Predict the Output
2 public class Final{
3 int lanif = 37;
4 int nafi = 21;
5 public static void main(String[] args){
6 final Final f = new Final();f.process2();
7 f = modify(f);f.process();
8 }
9 public static final Final modify(final Final f){
10 f.process();
11 Final f2 = new Final();
12 f2.process();return f2;
13 }
14 final void process(){
15 lanif = nafi + nafi;
16 System.out.print(lanif + " " + nafi + " ");
17 }
18 void process2(){
19 nafi = lanif % 2;
20 System.out.print(nafi + " " + lanif + " ");
21 }
22 }
Question 6
A) 1 37 2 1 42 21 42 21
B) 18 37 36 18 42 21 42 21
C) 1 37 2 1 1 37 2 1
D) Compilation error
1 //Predict the Output
2 public class Main
3 {
4 final static short i = 2;
5 public static int j = 0;
6
7 public static void main(String [] args)
8 {
9 for (int k = 0; k < 3; k++)
10 {
11 switch (k)
12 {
13 case i: System.out.print(" 0 ");
14 case i-1: System.out.print(" 1 ");
15 case i-2: System.out.print(" 2 ");
16 }
17 }
18 }
19 }
20
21
22
Question 7
A) 2 1 0 1 0 0
B) 0 1 2 1 2 2
C) 2 1 2 0 1 2
D) Compilation error
1 //Predict the Output
2 public class Final
3 {
4 int a = 30;
5 public static void main(String[] args)
6 {
7 final int assign;
8 Final b = new Final();
9 process(b);
10 System.out.println(b.a);
11 process(b);
12 assign = b.a;
13 System.out.println(assign);
14 }
15 public static void process(Final a)
16 {
17 a.a = a.a + 5;
18 }
19 }
20
21
22
Question 8
A) 35
40
B) 30
45
C) 40
D) Compilation error
1 //Predict the Output
2 public class Final
3 {
4 public static void main(String[] args)
5 {
6 final String result = "Focus";
7 final String assign;
8 Final f = new Final();
9 assign = "academy";
10 final String xchange = " ";
11 System.out.println(process(result, assign, xchange));
12 }
13 static String process(String a, String b, String xchange)
14 {
15 xchange = b + " " + a;
16 return xchange;
17 }
18 }
19
20
21
22
Question 9
A) Focus
B) Focus academy
C) academy Focus
D) Compilation error
1 //Predict the Output
2 public class Result
3 {
4 public static void main(String args[])
5 {
6 B b = new B();
7 System.out.println("x = " + b.getResult(0, 1));
8 }
9 }
10 class A
11 {
12 final public int getResult(int a, int b) { return a * b; }
13 }
14 class B extends A
15 {
16 public int getResult(int a, int b) { return a + b; }
17 }
18
19
20
21
22
Question 10
A) x = 1
B) x = 0
C) Runtime error
D) Compilation error
1 //Predict the Output
2 public final class FinalPersonClass {
3 private final String name;
4 private final int age;
5 public FinalPersonClass(final String name, final int age){
6 super();
7 this.name = name;
8 this.age = age;
9 }
10 public int getAge() {
11 return age;
12 }
13 public String getName(){
14 return name;
15 }
16 public static void main(String[] args) {
17 FinalPersonClass fpc = new FinalPersonClass("Arun", 24);
18 FinalPersonClass fc = new FinalPersonClass("Kiran", 25);
19 System.out.println(fpc.name + " - " + fpc.age);
20 System.out.println(fc.getName() + " - " + fc.getAge());
21 }
22 }
Question 11
A) Arun - 24
Kiran - 25
B) Arun - 24
Arun - 24
C) Kiran – 25
Kiran - 25
D) Compilation error
1 //Predict the Output
2 public class Circle
3 {
4 private double radius;
5 public Circle(double radius)
6 {
7 radius = radius;
8 }
9 }
10
11
12
13
14
15
16
17
18
19
20
21
22
Question 12
A) The program does not compile because Circle does not have a default
constructor
C) The program has a compilation error because it does not have a main
method.
D) The program has a compilation error because we cannot assign radius to radius.
1 //Predict the Output
2 public class Drive
3 {
4 static int i = demo();
5 static
6 {
7 System.out.print(i);
8 }
9 Drive()
10 {
11 System.out.print("String2");
12 }
13 public static void main(String args[])
14 {
15 System.out.print("String1");
16 }
17 static int demo()
18 {
19 System.out.print("Drive");
20 return 10;
21 }
22 }
Question 13
A) 10String1
B) Drive10String1
C) Drive10
D) Compilation error
1 //Predict the Output
2 class Static{
3 static int x;
4 static int y;
5 void add(int a, int b){
6 x = a + b;
7 y = x + b;
8 }
9 }
10 public class static_use
11 {
12 public static void main(String args[])
13 {
14
15 Static obj1 = new Static();
16 Static obj2 = new Static();
17 int a = 2;
18 obj1.add(a, a + 1);
19 obj2.add(5, a);
20 System.out.println(obj1.x + " " + obj2.y);
21 }
22 }
Question 14
A) 2 10
B) 7 9
C) 2 2
D) Compilation error
1 //Predict the Output
2 public class Methods
3 {
4 static int x = 100;
5 int y = 100;
6 public void increment()
7 {
8 x++; y++;
9 }
10 public static void main( String[] args )
11 {
12 Methods t1 = new Methods();
13 Methods t2 = new Methods();
14 t1.increment();
15 t2.increment();
16 System.out.println(t2.y);
17 System.out.println(Methods.x);
18 }
19 }
20
21
22
Question 15
A) 100 prints infinite time
100
B) 100
100
C) 101
102
D) Compilation error
1 //Predict the Output
2 public class Main
3 {
4 public static void square(int x)
5 {
6 System.out.println(x*x);
7 }
8 public static void main (String[] arg)
9 {
10 square(8);
11 }
12 }
13
14
15
16
17
18
19
20
21
22
Question 16
A) 8
B) 64
D) Compilation error
1 //Predict the Output
2 public class Main
3 {
4 void MethodOfClassA()
5 {
6 System.out.println("Method of ClassA");
7 }
8 }
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Question 17
A) Main method not found in class
B) Method of ClassA
C) Constructor prints sum, if two parameters are passed with object creation
A) private
B) protected
C) static
D) final
Question 20
If class A has add() function with protected access, and few other members in
public.Then class B inherits class A privately. Will the user will not be able to call
_________ from the object of class B.
A) Any member of class A
B) 100
1000
C) 1000
10
D) Compilation Error
Question 22
Which is valid in a class that extends class A?
class A
{
protected int method1(int a, int b)
{
return 0;
}
}
Question 22
A) private int method1(int a, int b) { return 0; }
B) i = 0
C) i = 3
D) Compilation error
1 //Predict the Output
2 public class Methods
3 {
4 public int Method()
5 {
6 static int i = 0;
7 i++;
8 return i;
9 }
10 public static void main(String args[])
11 {
12 Methods test = new Methods();
13 test.Method();
14 int j = test.Method();
15 System.out.println(j);
16 }
17 }
18
19
20
21
22
Question 24
A) 1
B) 0
C) 1 2 3 4 5
D) Compilation error
1 //Predict the Output
2 class Base
3 {
4 Base()
5 {
6 System.out.print("Focus");
7 }
8 }
9 public class Alpha extends Base
10 {
11 public static void main(String[] args)
12 {
13 new Alpha();
14 new Base();
15 }
16 }
17
18
19
20
21
22
Question 25
A) 0
B) Focus
C) FocusFocus
D) Compilation error
1 //Predict the Output
2 import java.util.*;
3 public class NewTrees extends NewTree
4 {
5 public static void main(String [] args)
6 {
7 NewTrees t = new NewTrees();
8 t.count();
9 }
10 }
11 protected class NewTree
12 {
13 void count()
14 {
15 for (int x = 0; x < 5; x++,x++ )
16 {
17 System.out.print(" " + x);
18 }
19 }
20 }
21
22
Question 26
A) 0 2
B) 0 2 4
B) 10.0
C) 0.0
D) Compilation error
1 //Predict the Output
2 class Super
3 {
4 public Integer getLength()
5 {
6 return new Integer(4);
7 }
8 }
9 public class Sub extends Super
10 {
11 public Long getLength()
12 {
13 return new Long(5);
14 }
15 public static void main(String[] args)
16 {
17 Super obj = new Super();
18 Sub sub = new Sub();
19 System.out.println(obj.getLength().toString() + "," +
20 sub.getLength().toString());
21 }
22 }
Question 28
A) 4.0
B) 4
C) 5.0
D) Compilation error
1 //Predict the Output
2 interface Count
3 {
4 short counter = 0;
5 void countUp();
6 }
7 public class TestCount implements Count
8 {
9 public static void main(String [] args)
10 {
11 TestCount t = new TestCount();
12 t.countUp();
13 }
14 public void countUp()
15 {
16 for (int x = 6; x>counter; x--, ++counter)
17 {
18 System.out.print(" " + counter);
19 }
20 }
21 }
22
Question 29
A) 1 2
B) 1 2 3
C) 1 2 3 4
D) Compilation error
1 //Predict the Output
2 public class Outer
3 {
4 public void someOuterMethod()
5 {
6 //Line 6
7 }
8 public class Inner { }
9 public static void main(String[] argv)
10 {
11 Outer ot = new Outer();
12 //Line 12
13 }
14 }
15
16
17
18
19
20
21
22
Question 30
A) new ot.Inner(); //At line 12