Sail E0 Webinar

MCQs

Total Questions : 9
Question 1.


What is the output of this program?


class c {
public void main( String[] args )
{
System.out.println( "Hello" + args[0] );
}
}
  1.    Hello c
  2.    Hello
  3.    Hello world
  4.    Runtime Error.
 Discuss Question
Answer: Option D. -> Runtime Error.

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



Question 2.


What is the output of this program?


class A {
final public int calculate(int a, int b) { return 1; }
}
class B extends A {
public int calculate(int a, int b) { return 2; }
}
public class output {
public static void main(String args[])
{
B object = new B();
System.out.print("b is " + b.calculate(0, 1));
}
}
  1.    b is : 2
  2.    b is : 1
  3.    Compilation Error.
  4.    An exception is thrown at runtime.
 Discuss Question
Answer: Option C. -> Compilation Error.

The code does not compile because the method calculate() in class A is final and so cannot 

be overridden by method of class b.


Question 3.


What is the output of this program?


class conversion {
public static void main(String args[])
{
double a = 295.04;
int b = 300;
byte c = (byte) a;
byte d = (byte) b;
System.out.println(c + " " + d);
}
}
  1.    38 43
  2.    39 44
  3.    295 300
  4.    295.04 300
 Discuss Question
Answer: Option B. -> 39 44

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


Question 4.


What is the output of this program?


class char_increment {
public static void main(String args[])
{
char c1 = 'D';
char c2 = 84;
c2++;
c1++;
System.out.println(c1 + " " + c2);
}
}
  1.    E U
  2.    U E
  3.    V E
  4.    U F
 Discuss Question
Answer: Option A. -> E U

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


Question 5.

What is Truncation is Java?


  1.    Floating-point value assigned to an integer type.
  2.    Integer value assigned to floating type.
  3.    Floating-point value assigned to an Floating type.
  4.    Integer value assigned to floating type.
 Discuss Question
Answer: Option A. -> Floating-point value assigned to an integer type.

None.


Question 6.

If an expression contains double, int, float, long, then whole expression will promoted into 

which of these data types?


  1.    long
  2.    int
  3.    double
  4.    float
 Discuss Question
Answer: Option C. -> double

If any operand is double the result of expression is double.


Question 7.

What is the error in this code?
byte b = 50;
b = b * 50;


  1.    b can not contain value 100, limited by its range.
  2.    * operator has converted b * 50 into int, which can not be converted to byte without casting.
  3.    b can not contain value 50.
  4.    No error in this code
 Discuss Question
Answer: Option B. -> * operator has converted b * 50 into int, which can not be converted to byte without casting.

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.


Question 8.

Which of these is necessary condition for automatic type conversion in Java?


  1.    The destination type is smaller than source type.
  2.    The destination type is larger than source type.
  3.    The destination type can be larger or smaller than source type.
  4.    None of the mentioned
 Discuss Question
Answer: Option B. -> The destination type is larger than source type.

None.


Question 9.

What is the prototype of the default constructor of this class?
public class prototype { }


  1.    prototype( )
  2.    prototype(void)
  3.    public prototype(void)
  4.    public prototype( )
 Discuss Question
Answer: Option D. -> public prototype( )

None.


Latest Videos

Latest Test Papers