Which of the following statements is true about this boolean expression? score < 0 and score >100

Welcome to Core Java Quiz. Java is an object-oriented programming language.

Core Java Quiz

In this quiz, you will be tested on Core Java basics and OOPS concepts. There are some code snippets too to test your basic Java coding skills.

Some of the questions have multiple answers. You can click on the “Reveal Answer” button for the correct answer and explanation.

Give it a try and share it with others if you like it.

1. Which of the below is valid way to instantiate an array in java?

A. int myArray [] = {1, 3, 5};
B. int myArray [] [] = {1,2,3,4};
C. int [] myArray = (5, 4, 3);
D. int [] myArray = {“1”, “2”, “3”};

Click to Reveal Answer

Correct Answer: A

int [] myArray = {“1”, “2”, “3”}; is invalid because String can’t be converted to an int.

int [] myArray = (5, 4, 3); is invalid because array elements should be defined in curly braces ({}).

int myArray [] [] = {1,2,3,4}; is invalid because myArray is a two-dimensional array whereas in this case it’s being defined as a one-dimensional array. The compiler will complain as Type mismatch: cannot convert from int to int[].

2. Which of the below are reserved keyword in Java?

A. array
B. goto
C. null
D. int

Click to Reveal Answer

**Correct Answer: B, D
**
The goto and int are reserved keywords in java. array and null are not keywords in java.

3. What will happen if we try to compile and run below program?

interface Foo{ int x = 10;}

public class Test { 
    public static void main(String[] args) { 
        Foo.x = 20; 
        System.out.println(Foo.x); 
    }
}

A. Prints 10
B. Prints 20
C. Compile Time Error
D. Runtime error because Foo.x is final.

Click to Reveal Answer

**Correct Answer: C
**
By default, any field of the interface is public, static, and final. So we can’t change is, hence compile-time error at the statement Foo.x = 20;.

4. What will be the output of the below program?

public class Test {
	public static void main(String[] args) {
		char c = 65;
		System.out.println("c = " + c);
	}
}

A. Compile Time Error
B. Prints “c = A”
C. Runtime Error
D. Prints “c = 65”

Click to Reveal Answer

**Correct Answer: B
**
Java compiler tries to automatically convert int to char. Since 65 gets converted to A, hence output will be “c = A”. The char values range is from u0000 to uffff. So char c = 65535; is valid but char c = 65536; will give compile time error.

5. What will be output of below program?

public class Test { 
    public void main(String[] args) {
        int x = 10*20-20; 
        System.out.println(x); 
    }
}

A. Runtime Error
B. Prints 180
C. Prints 0
D. Compile-time error.

Click to Reveal Answer

Correct Answer: A

Runtime error because main method is not static. The error message will be Main method is not static in class Test, please define the main method as: public static void main(String[] args)

6. What are the valid statements for static keyword in Java?

A. We can have static block in a class.
B. The static block in a class is executed every time an object of class is created.
C. We can have static method implementations in interface.
D. We can define static block inside a method.

Click to Reveal Answer

**Correct Answers: A, C
**
We can have static block in a class, it gets executed only once when class loads. From java 8 onwards, we can have static method implementations in interfaces.

7. Select all the core concepts of OOPS.

A. Abstraction
B. Inheritance
C. Interface
D. Polymorphism
E. Generics

Click to Reveal Answer

Correct Answers: A, B, D

OOPS core concepts are;

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
  5. Composition
  6. Association
  7. Aggregation

Read more at OOPS Concepts

8. Which of the following statements are true for inheritance in Java?

A. The “extend” keyword is used to extend a class in java.
B. You can extend multiple classes in java.
C. Private members of the superclass are accessible to the subclass.
D. We can’t extend Final classes in java.

Click to Reveal Answer

**Correct Answer: D
**
Inheritance is one of the core concepts in Java. You should be familiar with it. Please read the following articles to learn more about the answer choices - Inheritance in Java, Multiple Inheritance in Java.

9. What will be the output of below program?

package com.journaldev.java;

public class Test {
	public static void main(String[] args) {
		Super s = new Subclass();
		s.foo();
	}
}

class Super {
	void foo() {
		System.out.println("Super");
	}
}

class Subclass extends Super {
	static void foo() {
		System.out.println("Subclass");
	}
}

A. Compile time error
B. Super
C. Subclass
D. Runtime error

Click to Reveal Answer

**Correct Answer: A
**
Subclass foo() method can’t be static, it will give compile time error This static method cannot hide the instance method from Super.

10. What will be the output of below program?

package com.journaldev.java;

public class Test {
	public static void main(String[] args) {
		Subclass s1 = new Subclass();
		s1.foo(); // line 6
		Super s = new Subclass();
		s.foo(); // line 8
	}
}

class Super {
	private void foo() {
		System.out.println("Super");
	}
}

class Subclass extends Super {
	public void foo() {
		System.out.println("Subclass");
	}
}

A. Compile time error at line 6
B. Compile time error at line 8
C. Compile time error at both line 6 and 8
D. Works fine and prints “Subclass” two times.

Click to Reveal Answer

**Correct Answer: B
**
Compile time error at line 8 because Super class foo() method is private. The error message is The method foo() from the type Super is not visible.

11. What will be the output of below program?

import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		try {
			throw new IOException("Hello");
		} catch (IOException | Exception e) {
			System.out.println(e.getMessage());
		}
	}
}

A. Compile-time error
B. Prints “Hello”
C. Runtime Error

Click to Reveal Answer

Correct Answer: A

Compile-time error as The exception IOException is already caught by the alternative Exception.

12. What will be the output of below program?

public class Test {
	public static void main(String[] args) {
		String x = "abc";
		String y = "abc";
		x.concat(y);
		System.out.print(x);
	}
}

A. abcabc
B. abc
C. null

Click to Reveal Answer

**Correct Answer: B
**
x.concat(y); will create a new string but it’s not assigned to x, so the value of x is not changed.

13. Which of the below are unchecked exceptions in java?

A. RuntimeException
B. ClassCastException
C. NullPointerException
D. IOException

Click to Reveal Answer

**Correct Answer: A, B, C
**
RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor’s throws clause.

14. What will be the output of below program?

package com.journaldev.java;

import java.io.IOException;

public class Test {
	public static void main(String[] args) {
		try {
			throw new Exception("Hello ");
		} catch (Exception e) {
			System.out.print(e.getMessage());
		} catch (IOException e) {
			System.out.print(e.getMessage());
		} finally {
			System.out.println("World");
		}
	}
}

A. Compile-time error
B. Hello
C. Hello World
D. Hello Hello World

Click to Reveal Answer

**Correct Answer: A
**
Compile-time error Unreachable catch block for IOException. It is already handled by the catch block for Exception.

15. Which of the following statement(s) are true for java?

A. JVM is responsible for converting Byte code to the machine-specific code.
B. We only need JRE to run java programs.
C. JDK is required to compile java programs.
D. JRE doesn’t contain JVM

Click to Reveal Answer

Correct Answer: A, B, C

For a complete explanation, read JDK, JRE, and JVM.

16. Can we have two main methods in a java class?

A. Yes
B. No

Click to Reveal Answer

Correct Answer: A

This was a tricky question. We can have multiple methods having name as “main” in java through method overloading.

17. Which of the following statements are true about annotations in java?

A. @interface keyword is used to create custom annotation
B. @Override is a built-in annotation in java
C. Annotations can’t be applied to fields in a class.
D. @Retention is one of the meta annotation in java.
E. Java annotation information gets lost when class is compiled.

Click to Reveal Answer

**Correct Answer: A, B, D
**
For a complete explanation, read Java Annotations.

18. Which of the following statements are true about Enum in java?

A. All java enum implicitly extends java.lang.Enum class.
B. Java enum can implement interfaces.
C. We can create instance of enum using new operator.
D. Enums can’t be used in switch statements.
E. Enum constants are implicitly static and final.

Click to Reveal Answer

**Correct Answer: A, B, E
**
Read more at Enum in Java.

19. Which of the below are built-in class loaders in java?

A. Bootstrap Class Loader
B. Extensions Class Loader
C. Runtime Class Loader
D. System Class Loader

Click to Reveal Answer

Correct Answer: A, B, D

Read more at Classloaders in Java.

20. What will be the output of below program?

package com.journaldev.util;

public class Test {
	public static String toString() {
		System.out.println("Test toString called");
		return "";
	}

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

A. “Test toString called”
B. Compile-time error
C. “Test@7fh2bd8” (Object class toString() method is being called)

Click to Reveal Answer

Correct Answer: B

We will get a compile-time error because we can’t have an Object class method overridden with the static keyword. The Object class has toString() method. You will get a compile-time error as “This static method cannot hide the instance method from Object”.

21. What will be the output of below program?

public class Test {
	public static void main(String[] args) {
		String s1 = "abc";
		String s2 = "abc";
		System.out.println("s1 == s2 is:" + s1 == s2);
	}
}

A. s1 == s2 is:true
B. false
C. s1 == s2 is:false
D. true

Click to Reveal Answer

Correct Answer: B

The given statements output will be “false” because in java + operator precedence is more than == operator. So the given expression will be evaluated to “s1 == s2 is:abc” == “abc” i.e false.

Conclusion

I hope you liked the Core Java Quiz. If you think I have missed some important areas, let me know and I will add some more tricky quiz questions here.

Next Quiz: Java String Quiz

What is a Boolean expression quizlet?

Boolean expression. checks logic and provides a true/false value. decision logic. decisions performed or skipped based on condition.

What is an if statement used for quizlet?

"if" statement is a construct that enables a program to specify alternative paths of execution.

Which of the following library functions is used to validate the length of a string?

You can get the length of a string using the strlen function.

What is the first input value for the validation of a loop?

The first input operation—just before the loop — is called a priming read, and its purpose is to get the first input value that will be tested by the validation loop.