JAVA MCQ: JAVA Multiple Choice Questions and Answers

JAVA MCQ with answers and explanations for placement tests and job interviews. These solved JAVA MCQs are useful for the campus placement for all freshers including Engineering Students, MCA students, Computer and IT Engineers, etc.

Our JAVA MCQ (JAVA Multiple Choice Questions ) focuses on various parts of the JAVA  programming language and its concept. It will be useful for anyone learning JAVA Basics, Essentials, and/or Fundamentals. We will regularly update the quiz and most interesting thing is that questions come in a random sequence. So every time you will feel new questions.

You may have come across several Java courses during your search for learning the Java language. Our team of experts has carefully analyzed some Java courses for you. You can check the courses, Trial of some courses is free.

 

Guideline of JAVA MCQ:

This JAVA MCQ is intended for checking your JAVA programming knowledge. It takes 1 hour, 15 minutes to pass the JAVA MCQ. If you don’t finish the JAVA MCQ within the mentioned time, all the unanswered questions will count as wrong. You can miss the questions by clicking the “Next” button and return to the previous questions by the “Previous” button. Every unanswered question will count as wrong. MCQ on JAVA has features of randomization which feel you a new question set at every attempt.

In this JAVA MCQ, we have also implemented a feature that not allowed the user to see the next question or finish the JAVA quiz without attempting the current JAVA MCQ.

0 votes, 0 avg

You have 1 hours 15 minutes to take the JAVA MCQs

Your time has been Over.


JAVA MCQ

JAVA MCQ

JAVA MCQ: JAVA Multiple Choice Questions and Answers

1 / 75

What is the result of this code?

try{
    System.out.print("Hello World");
}catch(Exception e){
    System.out.println("e");
}catch(ArithmeticException e){
    System.out.println("e");
}finally{
    System.out.println("!")
}

 

2 / 75

Object-oriented programming is a style of programming where you organize your program around __ rather than __ and data rather than logic.

3 / 75

How does the keyword _volatile_ affect how a variable is handled?

4 / 75

Which access modifier makes variables and methods visible only in the class where they are declared?

5 / 75

Which operator would you use to find the remainder after division?

6 / 75

You get a NullPointerException. What is the most likely cause?

7 / 75

Add a Duck called "Waddles" to the ArrayList ducks.

public class Duck {
  private String name;
  Duck(String name) {}
}

 

8 / 75

Refactor this event handler to a lambda expression:

groucyButton.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        System.out.println("Press me one more time..");
    }
});

 

9 / 75

What method can be used to create a new instance of an object?

10 / 75

What is the output of this code?

class Main {
    public static void main (String[] args) {
        String message = "Hello world!";
        String newMessage = message.substring(6, 12)
            + message.substring(12, 6);
        System.out.println(newMessage);
    }
}

 

11 / 75

Using Java's Reflection API, you can use __ to get the name of a class and __ to retrieve an array of its methods.

12 / 75

Which statement is NOT true?

13 / 75

What code would you use to tell if "schwifty" is of type String?

14 / 75

If you encounter UnsupportedClassVersionError it means the code was **\_** on a newer version of Java than the JRE **\_** it.

15 / 75

Given the string "strawberries" saved in a variable called fruit, what would fruit.substring(2, 5) return?

16 / 75

Normally, to access a static member of a class such as Math.PI, you would need to specify the class "Math". What would be the best way to allow you to use simply "PI" in your code?

17 / 75

By implementing encapsulation, you cannot directly access the class's _ properties unless you are writing code inside the class itself.

18 / 75

What is the result of this code?

try {
    System.out.println("Hello World");
} catch (Exception e) {
    System.out.println("e");
} catch (ArithmeticException e) {
    System.out.println("e");
} finally {
    System.out.println("!");
}

 

19 / 75

Fill in the blank to create a piece of code that will tell whether int0 is divisible by 5: 

boolean isDivisibleBy5 = _____

 

20 / 75

Which type of variable keeps a constant value once it is assigned?

21 / 75

Which choice is a disadvantage of inheritance?

22 / 75

What is displayed when this code is compiled and executed?

public class Main {
  public static void main(String[] args) {
    int x = 5;
    x = 10;
    System.out.println(x);
  }
}

 

23 / 75

Declare and initialize an array of 10 ints.

24 / 75

How would you convert a String to an Int?

25 / 75

How can you achieve runtime polymorphism in Java?

26 / 75

What is the output of this code?

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

 

27 / 75

Which of the following can replace the CODE SNIPPET to make the code below print "Hello World"?

interface Interface1 {
    static void print() {
        System.out.print("Hello");
    }
}

interface Interface2 {
    static void print() {
        System.out.print("World!");
    }
}

 

28 / 75

Which letters will print when this code is run?

public static void main(String[] args) {
  try {
    System.out.println("A");
    badMethod();
    System.out.println("B");
  } catch (Exception ex) {
    System.out.println("C");
  } finally {
    System.out.println("D");
  }
}
public static void badMethod() {
  throw new Error();
}

 

29 / 75

Which statements about abstract classes are true?

1. They can be instantiated.
2. They allow member variables and methods to be inherited by subclasses.
3. They can contain constructors.

 

30 / 75

Which is the most reliable expression for testing whether the values of two string variables are the same?

31 / 75

What is the output of this code?

1: class Main {
2:   public static void main (String[] args) {
3:     int array[] = {1, 2, 3, 4};
4:     for (int i = 0; i < array.size(); i++) {
5:        System.out.print(array[i]);
6:     }
7:   }
8: }

 

32 / 75

Why are ArrayLists better than arrays?

33 / 75

What is the result of this code?

char smooch = 'x';
System.out.println((int) smooch);

 

34 / 75

Which are valid keywords in a Java module descriptor (module-infoJava)?

35 / 75

What does this code print?

System.out.print("apple".compareTo("banana"));

 

36 / 75

What is the result of this code?

1: int a = 1;
2: int b = 0;
3: int c = a/b;
4: System.out.println(c);

 

37 / 75

What statement returns true if "nifty" is of type String?

38 / 75

What is the output of this code?

class Main {
    public static void main(String[] args){
        int a = 123451234512345;
        System.out.println(a);
    }
}

 

39 / 75

What is the output of this code?

import java.util.*;
class Main {
  public static void main(String[] args) {
    List<Boolean> list = new ArrayList<>();
    list.add(true);
    list.add(Boolean.parseBoolean("FalSe"));
    list.add(Boolean.TRUE);
    System.out.print(list.size());
    System.out.print(list.get(1) instanceof Boolean);
  }
}

 

40 / 75

What is the result of this code?

1: class Main {
2: 		Object message(){
3: 			return "Hello!";
4: 		}
5: 		public static void main(String[] args) {
6: 			System.out.print(new Main().message());
7: 			System.out.print(new Main2().message());
8: 		}
9: }
10: class Main2 extends Main {
11: 	String message(){
12: 		return "World!";
13: 	}
14: }

 

41 / 75

What is a valid use of the hashCode() method?

42 / 75

What is a valid use of the hashCOde() method?

43 / 75

Given this code, which command will output "2"?

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

 

44 / 75

What type of variable can be assigned to only once?

45 / 75

What is the result of this code?

1: class MainClass {
2:  final String message(){
3:      return "Hello!";
4:  }
5: }

6: class Main extends MainClass {
7:  public static void main(String[] args) {
8:      System.out.println(message());
9:  }

10: String message(){
11:     return "World!";
12:  }
13: }

 

46 / 75

What is an IS-A relationship?

47 / 75

How do you write a foreach loop that will iterate over ArrayList<Pencil>pencilCase?

48 / 75

How would you fix this code so that it compiles?

public class Nosey {
  int age;
  public static void main(String[] args) {
    System.out.println("Your age is: " + age);
  }
}

 

49 / 75

What language construct serves as a blueprint containing an object's properties and functionality?

50 / 75

What method signature will work with this code?

boolean healthyOrNot = isHealthy("avocado");

 

51 / 75

What is the output of this code?

class Main {
  static int count = 0;
  public static void main(String[] args) {
    if (count < 3) {
      count++;
      main(null);
    } else {
      return;
    }
    System.out.println("Hello World!");
  }
}

 

52 / 75

What is the result of this code?

class Main {
    public static void main (String[] args){
        System.out.println(print(1));
    }
    static Exception print(int i){
        if (i>0) {
            return new Exception();
        } else {
            throw new RuntimeException();
        }
    }
}

 

53 / 75

Which functional interfaces does Java provide to serve as data types for lambda expressions?

54 / 75

Which is the most up-to-date way to instantiate the current date?

55 / 75

What is the output of this code?

class Main {
    public static void main (String[] args) {
        List list = new ArrayList();
        list.add("hello");
        list.add(2);
        System.out.print(list.get(0) instanceof Object);
        System.out.print(list.get(1) instanceof Integer);
    }
}

 

56 / 75

Which keyword lets you call the constructor of a parent class?

57 / 75

Given the following two classes, what will be the output of the Main class?

package mypackage;
public class Math {
    public static int abs(int num){
        return num < 0 ? -num : num;
    }
}
package mypackage.elementary;
public class Math {
    public static int abs (int num) {
        return -num;
    }
}

 

import mypackage.Math;
import mypackage.elementary.*;

class Main {
    public static void main (String args[]){
        System.out.println(Math.abs(123));
    }
}

 

58 / 75

What is not a java keyword

59 / 75

Given the following definitions, which of these expressions will NOT evaluate to true?

boolean b1 = true, b2 = false; int i1 = 1, i2 = 2;

 

60 / 75

How many times f will be printed?

public class Solution {
    public static void main(String[] args) {
        for (int i = 44; i > 40; i--) {
            System.out.println("f");
        }
    }
}

 

61 / 75

What kind of relationship does "extends" denote?

62 / 75

eclare a variable that holds the first four digits of Π

63 / 75

What is the output of this code?

import java.util.*;
class Main {
  public static void main(String[] args) {
    String[] array = {"abc", "2", "10", "0"};
    List<String> list = Arrays.asList(array);
    Collections.sort(list);
    System.out.println(Arrays.toString(array));
  }
}

 

64 / 75

What does the following code print?

String str = "abcde";
str.trim();
str.toUpperCase();
str.substring(3, 4);
System.out.println(str);

 

65 / 75

The runtime system starts your program by calling which function first?

66 / 75

Which keyword lets you use an interface?

67 / 75

Use the magic power to cast a spell

public class MagicPower {
    void castSpell(String spell) {}
}

 

68 / 75

What is the output of this code?

class Main {
    public static void main(String[] args){
       String message = "Hello";
       for (int i = 0; i<message.length(); i++){
          System.out.print(message.charAt(i+1));
       }
    }
}

 

69 / 75

What will this program print out to the console when executed?

import java.util.LinkedList;

public class Main {
    public static void main(String[] args){
        LinkedList<Integer> list = new LinkedList<>();
        list.add(5);
        list.add(1);
        list.add(10);
        System.out.println(list);
    }
}

 

70 / 75

How do you force an object to be garbage collected?

71 / 75

How many times will this code print "Hello World!"?

class Main {
    public static void main(String[] args){
        for (int i=0; i<10; i=i++){
            i+=1;
            System.out.println("Hello World!");
        }
    }
}

 

72 / 75

Java programmers commonly use design patterns. Some examples are the __, which helps create instances of a class, the __, which ensures that only one instance of a class can be created; and the __, which allows for a group of algorithms to be interchangeable.

73 / 75

You have an ArrayList of names that you want to sort alphabetically. Which approach would NOT work?

74 / 75

What can you use to create new instances in Java?

75 / 75

What does this code print?

public static void main(String[] args) {
    int x=5,y=10;
    swapsies(x,y);
    System.out.println(x+"="+y);
}

static void swapsies(int a, int b) {
    int temp=a;
    a=b;
    b=temp;
}

 

Your score is

The average score is 0%

0%

Recommended Articles for you: