Java MCQ Number 01062

Java MCQ Set 1

1. Which of the following can be operands of arithmetic operators?
a) Numeric
b) Boolean
c) Characters
d) Both Numeric & Characters

Answer

Answer:d [Reason:] The operand of arithmetic operators can be any of numeric or character type, But not boolean.

2. Modulus operator, %, can be applied to which of these?
a) Integers
b) Floating – point numbers
c) Both Integers and floating – point numbers.
d) None of the mentioned

Answer

Answer:c [Reason:] Modulus operator can be applied to both integers and floating point numbers. .

3. With x = 0, which of the following are legal lines of Java code for changing the value of x to 1?
1. x++;
2. x = x + 1;
3. x += 1;
4. x =+ 1;
a) 1, 2 & 3
b) 1 & 4
c) 1, 2, 3 & 4
d) 3 & 2

Answer

Answer: c [Reason:] Operator ++ increases value of variable by 1. x = x + 1 can also be written in shorthand form as x += 1. Also x =+ 1 will set the value of x to 1.

4. Decrement operator, −−, decreases value of variable by what number?
a) 1
b) 2
c) 3
d) 4

Answer

Answer: a [Reason:] None.

5. Which of these statements are incorrect?
a) Assignment operators are more efficiently implemented by Java run-time system than their equivalent long forms.
b) Assignment operators run faster than their equivalent long forms.
c) Assignment operators can be used only with numeric and character data type.
d) None

Answer

Answer: d [Reason:] None.

6. What is the output of this program?

  1.     class increment 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             double var1 = 1 + 5; 
  6.             double var2 = var1 / 4;
  7.             int var3 = 1 + 5;
  8.             int var4 = var3 / 4;
  9.             System.out.print(var2 + " " + var4);
  10.  
  11.         } 
  12.     }

a) 1 1
b) 0 1
c) 1.5 1
d) 1.5 1.0

Answer

Answer:c [Reason:] None
output:
$ javac increment.java
$ java increment
1.5 1

7. What is the output of this program?

  1.     class Modulus 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              double a = 25.64;
  6.              int  b = 25;
  7.              a = a % 10;
  8.              b = b % 10;
  9.              System.out.println(a + " "  + b);
  10.         } 
  11.     }

a) 5.640000000000001 5
b) 5.640000000000001 5.0
c) 5 5
d) 5 5.640000000000001

Answer

Answer: a [Reason:] Modulus operator returns the remainder of a division operation on the operand. a = a % 10 returns 25.64 % 10 i:e 5.640000000000001. Similarly b = b % 10 returns 5.
output:
$ javac Modulus.java
$ java Modulus
5.640000000000001 5

8. What is the output of this program?

  1.     class increment 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              int g = 3;
  6.              System.out.print(++g * 8);
  7.         } 
  8.     }

a) 25
b) 24
c) 32
d) 33

Answer

Answer:c [Reason:] Operator ++ has more preference than *, thus g becomes 4 and when multiplied by 8 gives 32.
output:
$ javac increment.java
$ java increment
32

9. Can 8 byte long data type be automatically type cast to 4 byte float data type?
a) True
b) False

Answer

Answer: a [Reason:] Both data types have different memory representation that’s why 8-byte integral data type can be stored to 4-byte floating point data type.

10. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 1;
  6.              int b = 2;
  7.              int c;
  8.              int d;
  9.              c = ++b;
  10.              d = a++;
  11.              c++;
  12.              b++;
  13.              ++a;
  14.              System.out.println(a + " " + b + " " + c);
  15.         } 
  16.     }

a) 3 2 4
b) 3 2 3
c) 2 3 4
d) 3 4 4

Answer

Answer: d [Reason:] None.
output:
$ javac Output.java
$ java Output
3 4 4

Java MCQ Set 2

1. Which of these operators is used to allocate memory to array variable in Java?
a) malloc
b) alloc
c) new
d) new malloc

Answer

Answer: c [Reason:] Operator new allocates block of memory specified by the size of array, and gives the reference of memory allocated to the array variable.

2. Which of these is an incorrect array declaration?

a) int arr[] = new int[5]
b) int [] arr = new int[5]
c) int arr[] = new int[5]
d) int arr[] = int [5] new
Answer

Answer: d [Reason:] Operator new must be succeeded by array type and array size.

3. What will this code print?
int arr[] = new int [5];
System.out.print(arr);
a) 0
b) value stored in arr[0].
c) 00000

Answer

Answer: d [Reason:] If we trying to print any reference variable internally, toString() will be called which is implemented to return the String in following form:

4. Which of these is an incorrect Statement?
a) It is necessary to use new operator to initialize an array.
b) Array can be initialized using comma separated expressions surrounded by curly braces.
c) Array can be initialized when they are declared.
d) None of the mentioned

Answer

Answer: a [Reason:] Array can be initialized using both new and comma separated expressions surrounded by curly braces example : int arr[5] = new int[5]; and int arr[] = { 0, 1, 2, 3, 4};

5. Which of these is necessary to specify at time of array initialization?
a) Row
b) Column
c) Both Row and Column
d) None of the mentioned

Answer

Answer: a [Reason:] None.

6. What is the output of this program?

  1.     class array_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             int array_variable [] = new int[10];
  6. 	    for (int i = 0; i < 10; ++i) 
  7.             {
  8.                 array_variable[i] = i;
  9.                 System.out.print(array_variable[i] + " ");
  10.                 i++;
  11.             }
  12.         } 
  13.     }

a) 0 2 4 6 8
b) 1 3 5 7 9
c) 0 1 2 3 4 5 6 7 8 9
d) 1 2 3 4 5 6 7 8 9 10

Answer

Answer: a [Reason:] When an array is declared using new operator then all of its elements are initialized to 0 automatically. for loop body is executed 5 times as whenever controls comes in the loop i value is incremented twice, first by i++ in body of loop then by ++i in increment condition of for loop.
output:
$ javac array_output.java
$ java array_output
0 2 4 6 8

7. What is the output of this program?

  1.     class multidimention_array 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int arr[][] = new int[3][];
  6.             arr[0] = new int[1];
  7.             arr[1] = new int[2];
  8.             arr[2] = new int[3];               
  9. 	    int sum = 0;
  10. 	    for (int i = 0; i < 3; ++i) 
  11. 	        for (int j = 0; j < i + 1; ++j)
  12.                     arr[i][j] = j + 1;
  13. 	    for (int i = 0; i < 3; ++i) 
  14. 	        for (int j = 0; j < i + 1; ++j)
  15.                     sum + = arr[i][j];
  16. 	    System.out.print(sum); 
  17.         } 
  18.     }

a) 11
b) 10
c) 13
d) 14

Answer

Answer: b [Reason:] arr[][] is a 2D array, array has been allotted memory in parts. 1st row contains 1 element, 2nd row contains 2 elements and 3rd row contains 3 elements. each element of array is given i + j value in loop. sum contains addition of all the elements of the array.
output:
$ javac multidimention_array.java
$ java multidimention_array
10

8. What is the output of this program?

  1.     class evaluate 
  2.     {
  3.         public static void main(String args[]) 
  4.             {
  5. 	        int arr[] = new int[] {0 , 1, 2, 3, 4, 5, 6, 7, 8, 9};
  6. 	        int n = 6;
  7.                 n = arr[arr[n] / 2];
  8. 	        System.out.println(arr[n] / 2);
  9.             } 
  10.     }

a) 3
b) 0
c) 6
d) 1

Answer

Answer: d [Reason:] Array arr contains 10 elements. n contains 6 thus in next line n is given value 2 printing arr[2]/2 i:e 2/2 = 1.
output:
$ javac evaluate.java
$ java evaluate
1

9. What is the output of this program?

  1.     class array_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             char array_variable [] = new char[10];
  6. 	    for (int i = 0; i < 10; ++i) 
  7.             {
  8.                 array_variable[i] = 'i';
  9.                 System.out.print(array_variable[i] + "");
  10.             }
  11.         } 
  12.     }

a) 1 2 3 4 5 6 7 8 9 10
b) 0 1 2 3 4 5 6 7 8 9 10
c) i j k l m n o p q r
d) i i i i i i i i i i

Answer

Answer: d [Reason:] None.
output:
$ javac array_output.java
$ java array_output
i i i i i i i i i i

10. What is the output of this program?

  1.     class array_output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}};
  6.             int sum = 0;
  7.             for (int i = 0; i < 3; ++i)
  8.                 for (int j = 0; j <  3 ; ++j)
  9.                     sum = sum + array_variable[i][j];
  10.             System.out.print(sum / 5);
  11.         } 
  12.     }

a) 8
b) 9
c) 10
d) 11

Answer

Answer: b [Reason:] None.
output:
$ javac array_output.java
$ java array_output
9

Java MCQ Set 3

1. Which of these is not a bitwise operator?
a) &
b) &=
c) |=
d) <=

Answer

Answer:d [Reason:] <= is a relational operator.

2. Which operator is used to invert all the digits in binary representation of a number?
a) ~
b) <<<
c) >>>
d) ^

Answer

Answer:a [Reason:] Unary not operator, ~, inverts all of the bits of its operand in binary representation.

3. On applying Left shift operator, <<, on an integer bits are lost one they are shifted past which position bit?
a) 1
b) 32
c) 33
d) 31

Answer

Answer: d [Reason:] The left shift operator shifts all of the bits in a value to the left specified number of times. For each shift left, the high order bit is shifted out and lost, zero is brought in from right. When a left shift is applied to an integer operand, bits are lost once they are shifted past the bit position 31.

4. Which right shift operator preserves the sign of the value?
a) <<
b) >>
c) <<=
d) >>=

Answer

Answer: b [Reason:] None.

5. Which of these statements are incorrect?
a) The left shift operator, <<, shifts all of the bits in a value to the left specified number of times
b) The right shift operator, >>, shifts all of the bits in a value to the right specified number of times
c) The left shift operator can be used as an alternative to multiplying by 2
d) The right shift operator automatically fills the higher order bits with 0

Answer

Answer: d [Reason:] The right shift operator automatically fills the higher order bit with its previous contents each time a shift occurs. This also preserves the sign of the value.

6. What is the output of this program?

  1.     class bitwise_operator 
  2.     {
  3.         public static void main(String args[])
  4.         {
  5.             int var1 = 42;
  6.             int var2 = ~var1;
  7.             System.out.print(var1 + " " + var2);     
  8.         } 
  9.     }

a) 42 42
b) 43 43
c) 42 -43
d) 42 43

Answer

Answer:c [Reason:] Unary not operator, ~, inverts all of the bits of its operand. 42 in binary is 00101010 in using ~ operator on var1 and assigning it to var2 we get inverted value of 42 i:e 11010101 which is -43 in decimal.
output:
$ javac bitwise_operator.java
$ java bitwise_operator
42 -43

7. What is the output of this program?

  1.     class bitwise_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 3;
  6.              int b = 6;
  7.  	     int c = a | b;
  8.              int d = a & b;             
  9.              System.out.println(c + " "  + d);
  10.         } 
  11.     }

a) 7 2
b) 7 7
c) 7 5
d) 5 2

Answer

Answer: a [Reason:] And operator produces 1 bit if both operand are 1. Or operator produces 1 bit if any bit of the two operands in 1.
output:
$ javac bitwise_operator.java
$ java bitwise_operator
7 2

8. What is the output of this program?

  1.     class leftshift_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {        
  5.              byte x = 64;
  6.              int i;
  7.              byte y; 
  8.              i = x << 2;
  9.              y = (byte) (x << 2)
  10.              System.out.print(i + " " + y);
  11.         } 
  12.     }

a) 0 64
b) 64 0
c) 0 256
d) 256 0

Answer

Answer:d [Reason:] None.
output:
$ javac leftshift_operator.java
$ java leftshift_operator
256 0

9. What is the output of this program?

  1.     class rightshift_operator 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int x; 
  6.              x = 10;
  7.              x = x >> 1;
  8.              System.out.println(x);
  9.         } 
  10.     }

a) 10
b) 5
c) 2
d) 20

Answer

Answer: b [Reason:] Right shift operator, >>, devides the value by 2.
output:
$ javac rightshift_operator.java
$ java rightshift_operator
5

10. What is the output of this program?

  1.     class Output 
  2.     {
  3.         public static void main(String args[]) 
  4.         {    
  5.              int a = 1;
  6.              int b = 2;
  7.              int c = 3;
  8.              a |= 4;
  9.              b >>= 1;
  10.              c <<= 1;
  11.              a ^= c;
  12.              System.out.println(a + " " + b + " " + c);
  13.         } 
  14.     }

a) 3 1 6
b) 2 2 3
c) 2 3 4
d) 3 3 6

Answer

Answer: a [Reason:] None.
output:
$ javac Output.java
$ java Output
3 1 6

Java MCQ Set 4

1. Which of these is necessary condition for automatic type conversion in Java?
a) The destination type is smaller than source type
b) The destination type is larger than source type
c) The destination type can be larger or smaller than source type
d) None of the mentioned

Answer

Answer: b [Reason:] None.

2. What is the prototype of the default constructor of this class?
public class prototype { }
a) prototype( )
b) prototype(void)
c) public prototype(void)
d) public prototype( )

Answer

Answer: d [Reason:] None.

3. What is the error in this code?
byte b = 50;
b = b * 50;
a) b can not contain value 100, limited by its range.
b) * operator has converted b * 50 into int, which can not be converted to byte without casting.
c) b can not contain value 50.
d) No error in this code

Answer

Answer: b [Reason:] While evaluating an expression containing int, bytes or shorts , the whole expression is converted to int then evaluated and result is also of type int.

4. If an expression contains double, int, float, long, then whole expression will promoted into which of these data types?
a) long
b) int
c) double
d) float

Answer

Answer: c [Reason:] If any operand is double the result of expression is double.

5. What is Truncation is Java?
a) Floating-point value assigned to an integer type
b) Integer value assigned to floating type
c) Floating-point value assigned to an Floating type
d) Integer value assigned to floating type

Answer

Answer: a [Reason:] None.

6. What is the output of this program?

  1.     class char_increment 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             char c1 = 'D';
  6.             char c2 = 84;
  7.             c2++;
  8.             c1++;
  9.             System.out.println(c1 + " "  + c2);
  10.         } 
  11.     }

a) E U
b) U E
c) V E
d) U F

Answer

Answer: a [Reason:] Operator ++ increments the value of character by 1. c1 and c2 are given values D and 84, when we use ++ operator their values increments by 1, c1 and c2 becomes E and U respectively.
output:
$ javac char_increment.java
$ java char_increment
E U

7. What is the output of this program?

  1.     class conversion 
  2.     {
  3.         public static void main(String args[]) 
  4.         {
  5.             double a = 295.04;
  6.             int  b = 300;
  7.             byte c = (byte) a;
  8.             byte d = (byte) b;
  9.             System.out.println(c + " "  + d);
  10.         } 
  11.     }

a) 38 43
b) 39 44
c) 295 300
d) 295.04 300

Answer

Answer: b [Reason:] Type casting a larger variable into a smaller variable results in modulo of larger variable by range of smaller variable. b contains 300 which is larger than byte’s range i:e -128 to 127 hence d contains 300 modulo 256 i:e 44.
output:
$ javac conversion.java
$ java conversion
39 44

8. What is the output of this program?

  1.     class A 
  2.     {
  3.         final public int calculate(int a, int b) { return 1; } 
  4.     } 
  5.     class B extends A 
  6.     { 
  7.         public int calculate(int a, int b) { return 2; } 
  8.     } 
  9.      public class output 
  10.      {
  11.         public static void main(String args[]) 
  12.         { 
  13.             B object = new B(); 
  14.             System.out.print("b is " + b.calculate(0, 1));  
  15.         } 
  16.     }

a) b is : 2
b) b is : 1
c) Compilation Error.
d) An exception is thrown at runtime.

Answer

Answer: c [Reason:] The code does not compile because the method calculate() in class A is final and so cannot be overridden by method of class b.

9. What is the output of this program, if we run as “java main_arguments 1 2 3”?

  1.     class main_arguments 
  2.     {
  3.         public static void main(String [] args) 
  4.         {
  5.             String [][] argument = new String[2][2];
  6.             int x;
  7.             argument[0] = args;
  8.             x = argument[0].length;
  9.             for (int y = 0; y < x; y++) 
  10.                 System.out.print(" " + argument[0][y]);              
  11.         }
  12.     }

a) 1 1
b) 1 0
c) 1 0 3
d) 1 2 3

Answer

Answer: d [Reason:] In argument[0] = args;, the reference variable arg[0], which was referring to an array with two elements, is reassigned to an array (args) with three elements.
Output:
$ javac main_arguments.java
$ java main_arguments
1 2 3

10. What is the output of this program?

  1.     class c 
  2.     {    
  3.         public void main( String[] args ) 
  4.         {  
  5.             System.out.println( "Hello" + args[0] ); 
  6.         } 
  7.     }

a) Hello c
b) Hello
c) Hello world
d) Runtime Error

Answer

Answer: d [Reason:] A runtime error will occur owning to the main method of the code fragment not being declared static.
Output:
$ javac c.java
Exception in thread “main” java.lang.NoSuchMethodError: main

Java MCQ Set 5

1. What is the numerical range of a char data type in Java?
a) -128 to 127
b) 0 to 256
c) 0 to 32767
d) 0 to 65535

Answer

Answer: d [Reason:] Char occupies 16-bit in memory, so it supports 2^16 i:e from 0 to 65535.

2. Which of these coding types is used for data type characters in Java?
a) ASCII
b) ISO-LATIN-1
c) UNICODE
d) None of the mentioned

Answer

Answer: c [Reason:] Unicode defines fully international character set that can represent all the characters found in all human languages. Its range is from 0 to 65536.

3. Which of these values can a boolean variable contain?
a) True & False
b) 0 & 1
c) Any integer value
d) true

Answer

Answer: a [Reason:] Boolean variable can contain only one of two possible values, true and false.

4. Which of these occupy first 0 to 127 in Unicode character set used for characters in Java?
a) ASCII
b) ISO-LATIN-1
c) None of the mentioned
d) ASCII and ISO-LATIN1

Answer

Answer: d [Reason:] First 0 to 127 character set in Unicode are same as those of ISO-LAIN-1 and ASCII.

5. Which one is a valid declaration of a boolean?
a) boolean b1 = 1;
b) boolean b2 = ‘false’;
c) boolean b3 = false;
d) boolean b4 = ‘true’

Answer

Answer: c [Reason:] Boolean can only be assigned true or false literals.

6. What is the output of this program?

  1.     class array_output {
  2.         public static void main(String args[]) 
  3.         {    
  4.             char array_variable [] = new char[10];
  5. 	    for (int i = 0; i < 10; ++i) {
  6.                 array_variable[i] = 'i';
  7.                 System.out.print(array_variable[i] + "" );
  8.                 i++;
  9.             }
  10.         } 
  11.     }

a) i i i i i
b) 0 1 2 3 4
c) i j k l m
d) None of the mentioned

Answer

Answer: a [Reason:] None.
output:
$ javac array_output.java
$ java array_output
i i i i i

7. What is the output of this program?

  1.     class mainclass {
  2.         public static void main(String args[]) 
  3.         {
  4.             char a = 'A';
  5.             a++;
  6. 	    System.out.print((int)a);
  7.         } 
  8.     }

a) 66
b) 67
c) 65
d) 64

Answer

Answer: a [Reason:] ASCII value of ‘A’ is 65, on using ++ operator character value increments by one.
output:
$ javac mainclass.java
$ java mainclass
66

8. What is the output of this program?

  1.     class mainclass {
  2.         public static void main(String args[]) 
  3.         {
  4.             boolean var1 = true;
  5. 	    boolean var2 = false;
  6. 	    if (var1)
  7. 	        System.out.println(var1);
  8. 	    else
  9. 	        System.out.println(var2);
  10.        } 
  11.     }

a) 0
b) 1
c) true
d) false

Answer

Answer: c [Reason:] None.
output:
$ javac mainclass.java
$ java mainclass
true

9. What is the output of this program?

  1.     class booloperators {
  2.         public static void main(String args[]) 
  3.         {
  4.             boolean var1 = true;
  5. 	    boolean var2 = false;
  6. 	    System.out.println((var2 & var2));
  7.         } 
  8.     }

a) 0
b) 1
c) true
d) false

Answer

Answer: d [Reason:] boolean ‘&’ operator always returns true or false. var1 is defined true and var2 is defined false hence their ‘&’ operator result is false.
output:
$ javac booloperators.java
$ java booloperators
false

10. What is the output of this program?

  1.     class asciicodes {
  2.         public static void main(String args[]) 
  3.         {
  4.             char var1 = 'A';
  5. 	    char var2 = 'a';
  6. 	    System.out.println((int)var1 + " " + (int)var2);
  7.         } 
  8.     }

a) 162
b) 65 97
c) 67 95
d) 66 98

Answer

Answer: b [Reason:] ASCII code for ‘A’ is 65 and for ‘a’ is 97.
output:
$ javac asciicodes.java
$ java asciicodes
65 97

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.