Hello World MCQ in Java

Q1) Find the output of the given Java program?

public class Main {
   public static void main(String args[]) {
      System.out.println("Hello World");
   }
}

a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error

View Answer Answer:- a) Hello World

Q2) Find the output of the given Java program?

public class Main {
   public static void main(String args[]) {
      System.out.println("Hello World ");
      System.out.println("Hello Know Program");
   }
}

a) Hello World
b) Know Program
c) Hello World Hello Know Program
d)
Hello World
Hello Know Program

View Answer Ans:- d)
Hello World
Hello Know Program

The println() method places a new line. Therefore the second String “Hello Know Program” will be printed in a new line.

Q3) What is the output of the given below program?

public class Main {
   public static void main(String args[]) {
      System.out.print("Hello World ");
      System.out.print("Hello Know Program");
   }
}

a) Hello World
b) Know Program
c) Hello World Hello Know Program
d)
Hello World
Hello Know Program

View Answer Answer:- c) Hello World Hello Know Program

The print() method doesn’t add any new line, therefore the second String “Hello Know Program” will be printed in the same line.

Q4) What is the output of the given below program?

public class Main {
   public static void main(String args[]) {
      System.out.print("Hello World ");
      System.out.println("Hello Know Program");
   }
}

a) Hello World
b) Know Program
c) Hello World Hello Know Program
d)
Hello World
Hello Know Program

View Answer Answer:- c) Hello World Hello Know Program

Q5) What is the output of the given below program?

public class Main {
   public static void main(String args[]) {
      System.out.println("Hello "" World");
   }
}

a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error

View Answer Answer:- d) Compile-time error

To concatenate or add two strings we should use the + operator. Here we will get the following compile time error,
error: ')' expected
System.out.println("Hello "" World");
^
1 error

Q6) Find the output of the given Java program?

public class Main {
   public static void main(String args[]) {
      System.out.println("Hello "+" World");
   }
}

a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error

View Answer Answer:- a) Hello World

In this program, to concatenate the two strings + operator is used. Hence “Hello “+” World” becomes “Hello World”.

Q7) Find the output of the given Java program?

public class Main {
   public static void main(String args[]) {
      System.out.println("Hello "*" World");
   }
}

a) Hello World
b) HelloWorld
c) Compiled Successfully, No Output.
d) Compile-time error

View Answer Answer:- d) Compile-time error

We can’t use * to concatenate the String.

Q8) What is the output of the given below program?

public class Main {
   public static void main(String args[]) {
      try {
         System.out.print("Hello");
      }
      catch(ArithmeticException e) {
         System.out.println("World!");
      }
   }
}

a) Hello World!
b) Hello
c) World!
d) Compiled Successfully, No Output.
e) Compile-time error

View Answer Answer:- b) Hello

Exception is not raised is the try block, therefore, the try block will be executed successfully, and the catch block will not be executed. The catch block will be executed only when an exception is raised in the try block.

Q9) What is the output of the given below program?

public class Main {
   public static void main(String args[]) {
      try {
        System.out.print("Hello" + " " + 1 / 0);
      }
      catch(ArithmeticException e) {
        System.out.println("World!");
      }
   }
}

a) Hello World!
b) Hello
c) World!
d) Compiled Successfully, No Output.
e) Compile-time error

View Answer Answer:- c) World!

Whenever we divide a number with 0 then it raises ArithmeticException. In try block exception is raised, therefore execution of try block will be terminated and catch block will be executed.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

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