JAVA Questions Answers


Hello friends in this post we are going to discuss about Java Multiple choice question with answer | Java question answers | Java MCQ with answers | Java Objective type questions with answer | Java Wipro Question answers | Trend NXT Myskillz dumps

Please Follow Below Question answers which are related to Java

If you are looking for more Dumps for MYSKILLZ Visit Here

58.What is the value of seasons.length for the following array?

String[] seasons = {“winter”, “spring”, “summer”, “fall”, };

 undefined

a) 4

b) 5

c) 22

Ans: b

59.Which of the following will declare an array and initialize it ?

a) Array a = new Array(5);

b) int array[] = new int [5];

c) int a[] = new int(5);

d)int [5] array;

Ans: b

60.Which of the following is an illegal declaration of array ?

a)  int [] myscore[];

b) char [] mychars;

c) Dog mydogs[7];

d) Dog mydogs[];

Ans: c

61.Which will legally declare, construct, and initialize an array?

a) int [] myList = {“5”, “8”, “2”};

b) int [3] myList = (5, 8, 2);

c) int myList[] [] = {5,8,2,0};

d) int [] myList = {5, 8, 2};

Ans: d

62.Which of these array declaration statements is not legal?

a) int[] i[] = { { 1, 2 }, { 1 }, {}, { 1, 2, 3 } };

b) int i[][] = new int[][] { {1, 2, 3}, {4, 5, 6} };

c) int i[4] = { 1, 2, 3, 4 };

d) int i[][] = { { 1, 2 }, new int[ 2 ] };

Ans:c

63.Which of the following is a legal declaration of a two-dimensional array of integers?

a) int[5][5]a = new int[][];

b) int a = new int[5,5];

c) int[]a[] = new int[5][];

d) int[][]a = new int[][5];

Ans: c

64.How would you declare and initialize the array to declare an array of fruits ?

a) String[] arrayOfFruits = {“apple”, “mango”, “orange”};

b) String[] arrayOfFruits= (“apple”, “mango”, “orange”);

c) String[] arrayOfFruits= [“apple”, “mango”, “orange”];

d) String[] arrayOfFruits = new String{“apple, mango, orange”};

Ans: a

65.What type parameter must the following method be called with?

int myMethod ( double[] ar )

{

    . . . .

}

a) An empty double array.

b) A reference to an array that contains elements of type double.

c) A reference to an array that contains zero or more elements of type int.

d) An array of any length that contains double and must be named ar

Ans: b

.

66.After the declaration:

char[] c = new char[100];

What is the value of c[50]?

a) 49

b) 50

c) ‘\u0020’

d) ‘\u0000’

Ans: d

67.Which will legally declare, construct, and initialize an array?

a) int [] myList = {“9”, “6”, “3”};

 b) int [3] myList = (9, 6, 3);

c) int myList[] [] = {9,6,3,0};

d) int [] myList = {9, 6, 3};

Ans: d

68.Given the following code snippet:

float average[] = new float[6];

Assuming the above declaration is a local variable in a method of a class, after the above statement is executed, which of the following statement is false ?

a) average.length is 6

b) average[0] is 0.0

c) average[5] is undefined

d) average[6] is undefined

Ans: c

69.Which one of the below expression is equivalent to 16>>2 ?

a) 16/4

b) 16/2

c) 16*2

Ans: a

70.Which of the following is correct?

a) 8 >> 2 gives 2

b) 16 >>> 2 gives 2

c) 4 << 2 gives 2

d) 2 << 1 gives 2

Ans: a

71.Which of the following is correct?

128>>> 1 gives

a) 32

b)64

c) -64

d) -32

Ans: b

72.What is the value of -32 % 6 ?

a)  5

b) -5

c) 2

d) -2

Ans: d

73.Which one of the following is a short-circuit operator ?

a)|

b) &&

c) &

d) ^

Ans: b

74.Given the following code snippet:

double sum = 10.0, price=100;

sum += price>=100 ? price*1.1 : price;

What value is placed in sum? Choose the most appropriate answer.

a) 90

b) 100

c) 110

d) 120

Ans: d

75.If x, y, and z are all integers, which expression will produce a runtime error?

NOTE: The expressions are always evaluated with all the integers having a value of 1.

a) z = x/y–;

b) z = -x/x;

c) z = y/x–;

d) z = y%–x

Ans: d

76.Given a variable x of type int ( which contains a positive value), which is the correct way of doubling the value of x, barring any wrapping of out-of-range intermediate values ?

a) x << 1;

b) x >> 1;

c) x >>> 1;

Ans: a

77.Suppose you have four int variables: x, y, z, and result.

Which expression sets the value of z to x if result has a value of 1, and the value of y to x otherwise?

a) x = (result == 1) ? z : y;

b) x = (result == 1) ? y : z;

c) x = (result == 1) : y ? z;

d) x = (result == 1) : z ? y;

Ans: a

78.Given a variable x of type int ( which can contain a negative value), which of these expressions always gives a positive number irrespective of the value of x?

a) x << 1;

b) x >> 1;

c) x >>> 1;

 d) x << 2;

Ans: c

79.Given:

    int x = 7;

    x <<= 2;

What best describes the second line of code?

a)  It assigns the value of 2 to x, and shifts it to left by one place.

b) It assigns the value to x after shifting x to 2 places left.

c) It assigns the value to x after shifting 2 to x places left.

d) It is invalid because there is no such operator as <<=.

Ans: b

80.Given the variables defined below:

int one = 1;

int two = 2;

char initial = ‘2’;

boolean flag = true;

Which one of the following is invalid?

a)  if( one == two ){}

b) switch( one ){}

c) switch( flag ){}

d) switch( initial ){}

Ans: c

81.Identify the shift operator that returns -1 as the value of the variable in the following statement:

int a= -4 MISSING OPERATOR 2;

a) >>>

b) >>

c) <<<

d) <<

Ans: b

82.public class TestOperator {

    public static void main(String[] args) {

        byte x = 0x0F;

        byte y = 0x08;

byte z = x & y;logical operations returns int

        System.out.println(z);

    }

}

What is the result?

a) 8

b) 15

c) 23

d) Compilation error

Ans: d

83.public class TestExpression {

    private static int value=0;

    private static boolean method2(int k) {

        value+=k;

        return true;

    }

    public static void method1(int index) {

        boolean b;

        b = index >= 15 && method2(30);

        b = index >= 15 & method2(15);

    }

public static void main ( String args[]) {

method1(0);

        System.out.println(value);

    }

}

What is the output?

a) 0

b) 15

c) 30

d) 45

Ans: b

84.public class TestCondition {

    public static void main (String… args) {

        int i=1;

        int j=2;

        int k=2;

        if ((i ^ j) && (j ^ k)) {

            System.out.println(“true”);

        }

        else {

            System.out.println(“false”);

        }

    }

}

What is the expected output ?

a) Prints true

b) Prints false

c)Compilation error occurs

d) Runtime error occurs

Ans: c

85.1. public class TestFloatDouble {

2.     public static void main(String[] args) {

3.         float f1 = 2.0f;

4.         double d1 = 4.0;

5.         double result = f1 * d1;

6.         System.out.println(result);

7.     }

8. }

What is the output ?

a) 8.0

b) Compilation error at Line 3

c) Compilation error at Line 4

d) Compilation error at Line 5

Ans: a

86.public class Test {

    public static void main(String[] args)

    {

        System.out.println( 6 ^ 4);

    }

}What is the output?

a) 1296

b) 24

c) 2

d) Compilation error

Ans: c

87.What will happen if you try to compile and run the following code?

int a = 200;

byte b = a;

System.out.println (“The value of b is ” + b );

a)  It will compile and print The value of b is 200

b) It will compile but cause an error at runtime

c) Compile-time error

d) It will compile and print The value of b is -56

Ans: c

88..Given:

public class TestOperator {

    int x=15;

    public void method(int x) {

        x+=x;

        System.out.println(x);

    }

    public static void main(String… args) {

        TestOperator t = new TestOperator();

        t.method(10);

    }

}

What is the output of the above code?

a) 10

b)20

c) 25

d) 30

Ans: b

89.1. public class TestLiterals {

2.     public static void main(String[] args) {

3.         float f1 = 2.0;

4.         float f2 = 4.0f;

5.         float result = f1 * f2;

6.         System.out.println(result);

7.     }

8. }

What is the output?

a) A value which is exactly 8.0

b)Compilation error at Line 3

c)Compilation error at Line 4

d) Compilation error at Line 5

Ans: b

90.public class TestChar {

    static double a; static float b; static int c; static char d;

    public static void main(String[] args) {

        a = b = c = d = ‘a’;

   System.out.println(a+b+c+d == 4 * ‘a’);

    }

}

What is the output?

a) true

b) false

c) Compile-time error

d) Run-time error

Ans: a

91.public class TestOperator {

    public static void main (String[] args) {

        int x = 2, y = 4;

        System.out.printf(“%d,%d”, (x ^ y), (y ^ x));

    }

}

What is the expected output ?

a) 8,8

b) 6,8

c) 6,6

d) 8,6

Ans: c

92.public class Test {

   private static int value =0;

    private static boolean method2(int k) {

        value+=k;

        return true;

    }

    public static void method1(int index) {

        boolean b;

        b = index < 10 | method2(10);

        b = index < 10 || method2(20);

    }

    public static void main ( String args[]) {

method1(0);

        System.out.println(value);

    }

}

What is the output?

a) 0

b)10

c) 20

d) 30

Ans: b

93.Given:

1.  public class B {

2.     Integer x;not initialized

3.     int sum;

4.     public B(int y) {

5.         sum=x+y;

6.         System.out.println(sum);

7.     }

8.     public static void main(String[] args) {

9.         new B(new Integer(23));

10.    }

11. }

What is the expected output ?

a) The value “23” is printed at the command line.

b) Compilation fails because of an error in line 9.

c) A NullPointerException occurs at runtime.

d) A NumberFormatException occurs at runtime.

Ans: c

94.public class TestOperator {

    public static void main(String[] args) {

        int x = 0x04;

        int y = 0x20;

        int z = x && y;

        System.out.println(z);

    }

}

What is the result?

a) 0

b) 24

c) 36

d) Compilation error

Ans: d

95.public class TestOperation {

    public static void main (String… args) {

        int a = 4;

        int b = 3;

        a += (–b + a * 3);

        System.out.printf(“a=%d,b=%d”,a,b);

    }

}

What is the value of a after this code is run?

a) a=19,b=3

b) a=18,b=2

c) a=19,b=1

d) a=18,b=3

Ans: b

96.public class TestIncrement {

    public static void main(String[] args)

    {

        int index=10;

int result=0;

        if (index++ > 10)

        {

result = index;

        }

        System.out.println(“index=” + index);

        System.out.println(“result=” + result);

}

What is the output?

a) index=10

result=0

b)  index=11

result=0

c) index=10

result=10

d) index=11

result=11

Ans: b

97.below:

if( val > 4 )

{ System.out.println( “Test A” );

}

else if( val > 9 )

{ System.out.println( “Test B” );

}

else System.out.println( “Test C” );

Which values of val will result in “Test C” NOT being printed?

a) val < 0

b) val = 0

c) 0 < val < 4

d) 4 < val < 9

Ans: d

98.public class TestIncrement {

    public static void main(String[] args)

    {

        int index=10;

        int result=0;

        if (++index > 10)

        {

            result = index;

        }

        System.out.println(“index=” + index);

        System.out.println(“result=” + result);

    }

}

What is the output?

a) index=10

result=0

b) index=11

result=0

c) index=11

result=10

d) index=11

result=11

Ans: d

99.public class Test{

    public static void main(String[] args) {

System.out.print((-1 & 0x1f) + “,” + (8 << -1));

    }

}

What is the result of attempting to compile and run the program?

a) 0,0

b)  0x1f,8

c) 31,16

d) 31,0

Ans: d

100.What is the value of x after this code is run?

int x = 3 ;

int y = 2 ;

x += (y + x * 2);

a) 9

b)  10

c) 11

Ans: c


101.Which of the following is illegal for a method declaration?

a) protected abstract void m1();

b) static final void m2(){}

c) transient private native void m3() {}

d) synchronized public final void m4() {}

Ans: c

102.Which one of these statements is true about constructors?

a) Constructors must not have arguments if the superclass b)constructor does not have arguments.

c) Constructors are inherited.

d)Constructors cannot be overloaded.

e)The first statement of every constructor is a legal call to the super() or this()method.

Ans: e

103.Here is a method definition:

int compute( int a, double y ){ . . . .}

Which of the following has a different signature?

a) int compute( int sum, double value ){ . . . .}

b) double compute( int a, double y ){ . . . .}

c) double compute( int sum, double y ){ . . . .}

d) int compute( int a, int y ){ . . . .}

Ans: d

104.Which one of the following is not a legal method declaration?

a) static final void m2(){}

b) transient private native void m3() {}

c) synchronized public final void m4() {}

d) private native void m5();

Ans: b

105.In a constructor, where can you place a call to the super class constructor?

a) The first statement in the constructor

b) The last statement in the constructor

c) You can’t call super in a constructor

Ans: a

106.Which one of the below statements is true?

a) When a class has defined constructors with parameters, the compiler does not create a default no-args constructor.

b) When a constructor is provided in a class, a corresponding destructor should also be provided.

c)The compiler always creates the default no-args constructor for every class.

d) The no-args constructor can invoke only the no-args constructor of the superclass. It cannot invoke any other constructor of the superclass.

Ans: a

107.Which of the following techniques can be used to prevent the instantiation of a class by any code outside of the class?

a) Do not declare any constructors.

b) Do not use a return statement in the constructor.

c) Declare all constructors using the keyword void to indicate that nothing is returned.

d) Declare all constructors using the private access modifier.

Ans: d

108.Which one of the following is legal declaration for nonnested classes and interfaces?

a) final abstract class Test {}

b) public static interface Test {}

c) final public class Test {}

d) protected interface Test {}

Ans: c

109.What is a method’s signature?

a)The signature of a method is the name of the method and the type of its return value.

b) The signature of a method is the name of the method and the names of its parameters.

c) The signature of a method is the name of the method and the data types of its parameters.

d) The signature of a method is the name of the method, its parameter list, and its return type.

Ans: c

110.public class MethodTest {

    public void methodSam( int a, float b, byte c) {}

}

Which of the following is considered as overloaded methodSam ?

a) private int methodSam( int a, float b, byte c) {}

b) private int methodSam( float a, int b, byte c) {return b;}

c) private float methodSam( int a, float b, byte c) {return b;}

d) public void methodSam( int x, float y, byte z) {}

Ans: d

111.Which one of the following is not a legal declaration for top level classes or interfaces ?

a) public abstract interface Test {}

b) final abstract class Test {}

c) abstract interface Test {}

d) public abstract class Test {}

Ans: b

112.A constructor is used to

a) Free memory

b) Initialize a newly created object.

c)  Import packages

d) Clean up the object

Ans: b

113.public class Constructor {

    public Constructor (int x, int y, int z)

    {}

}

Which of the following is considered as overloaded constructor?

a) Constructor() {}

b) protected int Constructor(){}

c) private Object Constructor() {}

d) public void Constructor(int x, int y, byte z){}

Ans: a

114.If MyProg.java were compiled as an application and then run from the command line as

   java MyProg I like myprogram

What would be the value of args[1] inside the main( ) method?

a) MyProg

b) I

c) like

d) 4

Ans: c

115.Given the following,

1. long test( int x, float y) {

2.

3. }

Which one of the following line inserted at line 2 would not compile?

a) return (long) y;

b) return (int) 3.14d;

c)return ( y / x );

d)return x / 7;

Ans: c

116.Which one of the following is generally a valid definition of an application’s main() method ?

a) public static void main();

b) public static void main( String args );

c) public static void main( String[] args );

d) public static void main( Graphics g );

Ans: c

117.Consider the following code segment and select the correct statement:

1. class Test {

2.     final int tst;

3.     final int w = 0;

4.

5.     Test() {

6.         tst = 1;

7.     }

8.

9.     Test(int x) {

10.        tst = x;

11.    }

12. }

a) The code fails to compile because a class cannot have more than 1 constructor.

b) The code fails to compile because the class Test has no constructors.

c) The code compiles correctly without any warnings or errors.

d) The code fails to compile because an attempt is made to initialise a final variable at lines 6 and 10.

Ans: d

118.Given the following,

1. class A {

2.     public int foo;

3. }

4. public class B extends A {

5.     private int bar;

6.     public void setBar(int b) {

7.         bar = b;

8.     }

9. }

Which is true about the classes described above?

a)Class A is tightly encapsulated.

b) Class B is tightly encapsulated.

c) Classes A and B are both tightly encapsulated.

d) Neither class A nor class B is tightly encapsulated.

Ans: b

119.Given the following,

1. public class Barbell {

2.     public int getWeight() {

3.         return weight;

4.     }

5.     public void setWeight(int w) {

6.         weight = w;

7.     }

8.     public int weight;

9. }

Which is true about the class described above?

a) Class Barbell is tightly encapsulated.

b) Line 2 is in conflict with encapsulation.

c) Line 5 is in conflict with encapsulation.

d) Line 8 is in conflict with encapsulation.

Ans: d

120.Examine the following code:

String str = “Hot Java”;

boolean bValue = str instanceof String;

What value is placed in bValue?

a) true

b) false

c)”Hot Java”

d) null

Ans: a

121.class A {

    A() { }

    void display() {

        System.out.println(“display of A called”);

    }

}

class B {

    B() { }

    void display() {

        System.out.println(“display of B called”);

    }

}

public class C extends A, B {

    public static void main(String[] args) {

        C c = new C();

        c.display();

    }

}

What is the output ?

a) Compilation error is generated

b) display of A called

display of B called

c) display of B called

display of A called

d)order of output is not predictable and can come in any order

Ans: a

122.Given the following,

1. import java.util.*;

2. public class NewTreeSet2 extends NewTreeSet {

3.     public static void main(String [] args) {

4.         NewTreeSet2 t = new NewTreeSet2();

5.         t.count();

6.     }

7. }

8. protected class NewTreeSet {

9.     void count() {

10.        for (int x = 0; x < 7; x++,x++ ) {

11.            System.out.print(” ” + x);

12.        }

13.    }

14. }

What is the result?

a)  0 2 4

b) 0 2 4 6

c) Compilation fails at line 4

d) Compilation fails at line 8

Ans: d

123.Given:

1. class Fruit {

2.     private String name;

3.     public Fruit(String name) { this.name = name; }

4.     public String getName() { return name; }

5. }

6. public class MyFruit extends Fruit {

7.     public void displayFruit() { }

9. }

Which of the following statement is true?

a) The code will compile if public MyFruit() { Fruit(); } is added to the MyFruit class.

b) The code will compile if public Fruit() { MyFruit(); } is added to the Fruit class.

c) The code will compile if public Fruit() { this(“apple”); } is added to the Fruit class.

d) The code will compile if public Fruit() { Fruit(“apple”); } is added to the Fruit class.

Ans: c

124.Given the following,

1.

2. public class NewTreeSet extends java.util.TreeSet{

3.     public static void main(String [] args) {

4.         java.util.TreeSet t = new java.util.TreeSet();

5.         t.clear();

6.     }

7.     public void clear() {

8.         TreeMap m = new TreeMap();

9.         m.clear();

10.    }

11. }

Which statement added at line 1, allow the code to compile?

a) No statement is required

b) import java.util.*;

c) import.java.util.Tree*;

d) import java.util.*Map;

Ans: b

125.Given the following,

public class TestConstructor extends Object

{

    TestConstructor()

    {

        super();

        this(10);

    }

    TestConstructor(int i)

    {

        this(i, 11);

    }

    TestConstructor(int i, int j)

    {

        System.out.println(“i=” + i + ” j=” + j);

    }

public static void main(String[] args)

    {

        TestConstructor tc = new TestConstructor();

    }

}

What will be the output?

a)  No output

b) i=10 j=11

c)Compilation error

d) Runtime error

Ans: c

126.Given the following,

1. import java.util.*;

2. class Ro {

3.     Object[] testObject() {

4.

5.

6.     }

7. }

Which one of the following code fragments inserted at lines 4, 5 will not compile?

a) return null;

b) Object t = new Object();

return t;

c) Object[] t = new Object[10];

return t;

d) Object[] t = new Integer[10];

return t;

Ans: b

127.Given the following,

1. public class ThreeConst {

2.     public static void main(String [] args) {

3.         new ThreeConst();

4.     }

5.     public void ThreeConst(int x) {

6.         System.out.print(” ” + (x * 2));

7.     }

8.     public void ThreeConst(long x) {

9.         System.out.print(” ” + x);

10.   }

11.

12.    public void ThreeConst() {

13.        System.out.print(“no-arg “);

14.    }

15. }

What is the result?

a) 8 4 no-arg

b) no-arg 8 4

c) Compilation fails.

d) No output is produced.

Ans: d

128.Given the following,

1. class Dog {

2.     Dog(String name) { }

3. }

If class Beagle extends Dog, and class Beagle has only one constructor, which of the following could be the legal constructor for class Beagle?

a) Beagle() { }

b) Beagle() { super(); }

c)Beagle() { super(“fido”); }

d) No constructor, allow the default constructor to get generated automatically.

Ans: c

129.Given the following,

1. public class CheckType {

2.     int check() {

3.

4.         return y;

5.     }

6.     public static void main(String [] args) {

7.         CheckType c = new CheckType();

8.         int x = c.check();

9.     }

10.}

Which line of code, inserted independently at line 3, will not compile?

a) short y = 7;

b) int y = (int) 7.2d;

c) Long y = 7;

d) int y = 0xface;

Ans: c

130.Given the following,

class TestFooBar {

public static Foo f = new Foo();

public static Foo f2;

public static Bar b = new Bar();

5.

public static void main(String [] args) {

for (int x=0; x<4; x++) {

f2 = getFoo(x);

f2.react();

}

}

static Foo getFoo(int y) {

if ( 0 == y % 2 ) {

return f;

} else {

return b;

}

}

}

20.

21. class Bar extends Foo {

22.     void react() { System.out.print(“Bar “); }

23. }

24.

25. class Foo {

26.     void react() { System.out.print(“Foo “); }

27. }

What is the result?

a) Bar Bar Bar Bar

b)Foo Bar Foo Bar

c) Foo Foo Foo Foo

d) Compilation fails.

Ans: b

131.Consider the following piece of code:

class A {

    int x = 0;

    A(int w) {

        x = w;

    }

}

class B extends A {

    int x = 0;

    B(int w) {

        x = w + 1;

    }

}

a) The code compiles correctly.

b) The code fails to compile, because both class A and B do not have valid constructors.

c) The code fails to compile because there is no default no-args constructor for class A.

d) The code fails to compile because there is no default no-args constructor for class B.

Ans: c

132.Given the following,

1. class Base {

2.     Base() {

3.         System.out.println(“Base constructor invoked…”);

4.     }

5. }

6.

7. public class Derived extends Base {

8.     Derived() {

9.         System.out.println(“Derived constructor invoked…”);

10.    }

11.

12.    public static void main (String[] args) {

13.        Base b = new Derived();

14.    }

15.}

What is the output ?

a)Base constructor invoked…

 Derived constructor invoked…

b) Base constructor invoked…

c) Derived constructor invoked…

d) Derived constructor invoked…

Base constructor invoked…

Ans: a

133.Given the following,

1. public class ThreeConst {

2.     public static void main(String [] args) {

3.         new ThreeConst(4L);

4.     }

5.     public ThreeConst(int x) {

6.         this();

7.         System.out.print(” ” + (x * 2));

8.     }

9.     public ThreeConst(long x) {

10.       this((int) x);

11.       System.out.print(” ” + x);

12.    }

13.

14.    public ThreeConst() {

15.        System.out.print(“no-arg “);

16.    }

17. }

What is the result?

a)  4 8

b) 8 4 no-arg

c) no-arg 8 4

d) Compilation fails.

Ans: c

134.Given the following,

1.

2. public class MyHashSet extends java.util.HashSet{

3.     public static void main(String [] args) {

4.         java.util.HashSet hs = new java.util.HashSet();

5.         hs.clear();

6.     }

7.     public void hmClear() {

8.         HashMap hm = new HashMap();

9.         hm.clear();

10.    }

11. }

Which statement added at line 1, allow the code to compile?

a) import java.util.*;

b) import java.util.*Map;

c) import java.util.Hash*;

d) No statement is required

Ans: a


Leave a Reply

Your email address will not be published. Required fields are marked *