Core Java Quiz

Filed Under: Java


Welcome to Core Java Quiz. Java is an object-oriented programming language. In this quiz, you will be tested on core java basics and OOPS concepts. Give it a try and share with others if you like it. 🙂

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

Correct! Wrong!

int [] myArray = {"1", "2", "3"}; is invalid because String can't be converted to int. int [] myArray = (5, 4, 3); is invalid because array elements are defined in curly braces ({}). int myArray [] [] = {1,2,3,4}; is invalid because myArray is a two dimensional array whereas it's being defined as one dimensional array. Compiler will complain as Type mismatch: cannot convert from int to int[].

Which of the below are reserved keyword in java?

Please select 2 correct answers

Correct! Wrong!

goto and int are reserved keywords in java. array and null are not keywords in java.

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);
		
	}

}

Correct! Wrong!

By default, any field of the interface is public, static and final. So we can't change is, hence compile-time error at line 8 statement Foo.x = 20;.

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);
	}

}

Correct! Wrong!

Java compiler tries to automatically convert int to char. Since 65 gets converted to A, hence output will be "c = A". Note that char are limited to values from \u0000 to \uffff. So char c = 65535; is valid but char c = 65536; will give compile time error.

What will be output of below program?

public class Test {

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

}

Correct! Wrong!

Runtime error because main method is not static.


pankaj:bin pankaj$ java Test
Error: Main method is not static in class Test, please define the main method as:
   public static void main(String[] args)

What are the valid statements for static keyword in java?

Please select 2 correct answers

Correct! Wrong!

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. Further Reading: static keywords in java java 8 interface changes

Select all the core concepts of OOPS.

Please select 3 correct answers

Correct! Wrong!

OOPS core concepts are;

  1. Abstraction
  2. Encapsulation
  3. Polymorphism
  4. Inheritance
  5. Composition
  6. Association
  7. Aggregation
Read more at OOPS Concepts

Which of the following statements are true for inheritance in java?

Correct! Wrong!

Inheritance is one of the core concepts in Java. You should be familiar with it. Please read below articles to learn more about the answer choices. Inheritance in Java Multiple Inheritance in Java

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");
	}

}

Correct! Wrong!

Subclass foo() method can't be static, it will give compile time error This static method cannot hide the instance method from Super.

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 7
		
		Super s = new Subclass();
		s.foo(); // line 10
	}
}

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

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

Correct! Wrong!

Compile time error at line 10 because Super class foo() method is private. Below code snippet shows the compilation error for Java 9.


$javac Test.java 
Test.java:10: error: foo() has private access in Super
		s.foo();
		 ^
1 error
$

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());
		}
	}
}

Correct! Wrong!

Compile time error as The exception IOException is already caught by the alternative Exception Read Java Catch multiple exceptions

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);
	}
}

Correct! Wrong!

x.concat(y); will create a new string but it's not assigned to x, so value of x is not changed.

Which of the below are unchecked exceptions in java?

Please select 3 correct answers

Correct! Wrong!

RuntimeException and its subclasses are unchecked exceptions. Unchecked exceptions do not need to be declared in a method or constructor's throws clause. Read more at Exception Handling in Java

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");
		}
	}
}

Correct! Wrong!

Compile-time error Unreachable catch block for IOException. It is already handled by the catch block for Exception.

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

Please select 3 correct answers

Correct! Wrong!

Read more at JDK, JVM, JRE

Can we have two main methods in a java class?

Correct! Wrong!

This was a tricky question. We can have multiple main methods in java through method overloading.

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

Please select 3 correct answers

Correct! Wrong!

Read more at Java Annotations

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

Please select 3 correct answers

Correct! Wrong!

Read more at Java Enum

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

Please select 3 correct answers

Correct! Wrong!

Complete details at Java Classloader

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());
	}
}

Correct! Wrong!

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

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);
	}
}

Correct! Wrong!

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.

Core Java Quiz
Quiz Results: Pass
You have a decent understanding of the Core Java.
Quiz Result: Distinction
Awesome! You have a deep understanding of the Core Java and its internal implementation. You should now try Java String Quiz.
Quiz Result: Poor
You lack the basics of Core Java. Please go through the Core Java Tutorials to improve your understanding about Java.

Comments

  1. Achyut says:

    in question 4 i have copy your code and getting out is ‘c = A’ i have selected that option only but it says wrong option

    1. Pankaj says:

      Sorry about that, some error happened when I was editing the question. I have fixed it.

  2. Naseem Ahamed says:

    Thanks for the quiz. 15th question blew my mind!

  3. Nate says:

    I’m not sure what java version this was written against, but you most certainly can use Enums in switch statements. You couldn’t use strings in switch statements before Java 7, but even before that you could use Enums. Even the link in the description for why “Enums can’t be used in switch statements.” says that they can be used in switch statements.

    1. Pankaj says:

      Yes, you are right and so does the question. You have to select statements that are true for Enum.

  4. Tom says:

    Java Runtime Environment (JRE) contains JVM, class libraries, and other supporting files.

  5. Khyn Harold Jay Antoque says:

    Do you have a source code when making this quiz, did you use php for this?

  6. nisha says:

    Hello Pankaj,
    Your site has some problem. I have attempted only 5 questions, and finished my quiz.but it showing all the questions are attempted. with incorrect answer.

    1. Pankaj says:

      Yes, when you finished the quiz. Any question you didn’t attempted will be considered as incorrectly answered. Just like any exams, if you won’t provide answer to a question, you won’t get any marks for it.

  7. Enrico says:

    Are you sure about question 8? I just copied and paste your code on IntelliJ and I got compilation error.

  8. Hubert says:

    Question 6>> WRONG>> My output is Super! I get an error, too, but the question was >> What will be the output of below program?

    1. Pankaj says:

      Did you run the code in Eclipse, use command line and you will see that output is correct? Check this thread to understand why you got the output.
      https://stackoverflow.com/questions/49633887/compiler-error-but-program-executes-fine

    2. Ram Sharan says:

      Looks like Pankaj sir has made some changes to this set of questions. Earlier I was completely correct, but now the marked answers are showing as the wrong answer but explanation is correct. I also did google and tried output code in eclipse. I am double sure about this mistake.
      Pankaj sir plz cross check this issue. The explanation shows correct answers and I have marked same answer but it’s highlighted by red color.

  9. Eswar says:

    One of the questions were to select java core concepts from the options. One of the options were ‘Generics’ which was actually a link to Generics page. When I accidentally clicked on it, it took me to another page. Due to this, I could not complete the quiz. Please remove that link so that it won’t happen again for others.

    1. Pankaj says:

      Thanks for letting us know, i have fixed it.

  10. kaka says:

    This is a good warm up quiz. I realised I still have so many things to do

  11. Raviraj BK says:

    It was very good quiz!! 🙂 Awesome work

  12. koreanOppa says:

    This was an interesting quiz for me. I just found out I need more practice in Java.

  13. Yannick loic says:

    Very instructive

  14. java reader says:

    Good questions

  15. Jawad Ali says:

    Thanks a lot for the effort put to make this quiz. Really helped refresh all the basics.

    Now that I have got a taste for a quality quiz, I would love to see similar quiz for more advanced topics like Collections and Threads.

    Once again, Great work! Thanks.

  16. Sujoy says:

    Good questions. The best part was the explanation of the quiz answers at the end for review.

  17. Anita says:

    Nice to learn via Quiz!

  18. Rhushikesh says:

    Where to start quiz. I am not able to see any link

    1. Pankaj says:

      I was editing the Quiz questions, so it was unavailable for some time. It’s back up again now, please give it a try. Thanks.

  19. Peter Alexay says:

    Good questions! Thanks for your quiz.

  20. Svetik says:

    Thanks for this great test! It showed me a lot of gaps in my knowledge! It would be great to have some more ! 🙂

  21. Luis Bravo says:

    Greetings from Venezuela, good job

  22. vikash says:

    doing good job sir

  23. Niranjan Kumaar says:

    Hi
    I need SQL program like many to one, one to one, one to many on different different kind. who has been asked in interview …

  24. Alex says:

    Description of answer to question 11 about multiple exceptions handling depends on language level, although it’s сompile time error in any case.
    java multi-catch statement is not supported in -source 1.6

    1. Pankaj says:

      Hi Alex,

      Any programming test always assume the latest version. For example, if you have a lambda program, you shouldn’t say that output depends on whether you are on java 8 or lower.

  25. Aly says:

    Good job! Thanks.
    Only one remark:
    question ” Can we have two main methods in a java class?” The answer no is incorrect.
    Because yes, we can have 2 methods, for example:
    public class A {
    public static void main( Stting [] args) {}
    public void main( int[] args) {}
    }
    Why not?)

    1. Pankaj says:

      I think you didn’t checked the question properly, you are iterating the same thing in the question explanation.

  26. rahul malgujar says:

    good job…a very good medium for begineers .

  27. Miguel Torres says:

    Excellent JAVA Core quiz. I realized that I need to practice more the basic topics, We apreciate all your efforts. Thanks

    Regards

  28. Yevgeny says:

    The quiz is very good but IMO it contains mistakes. “null” cannot be used as a name of variable.Therefore, it is a reserved word in Java. This is output from javac.

    test.java: error: expected
    String null;
    ^

      1. pa1 says:

        true, false, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.

  29. Williams Ramos Horizonte says:

    excelent

  30. Unamuno says:

    Even if you use a multiple choice approach, the answer may not be true or false.
    Example given: You asked for java keywords, and rated my answer incorrect. But null is no keyword, nbut the null literal. I noticed several of such things but did’nt took the time to write everything down.
    I’ll visit again and try to give more comments.
    Thanks for your efforts, it helps in preparing very simple tasks.
    But they’re far away from what I excpect fromwhat I expect students at my university to master once they have terminated their bachelor courses.

    1. Pankaj says:

      The question and answers for java keywords are correct, let me drop you an email for this.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages