Test Your Core Java Knowledge: A Comprehensive Quiz
In this post, we will challenge your understanding of Core Java and key OOP (Object-Oriented Programming) principles. The quiz is designed to test your theoretical knowledge as well as your coding skills with some practical examples. Feel free to check your answers by revealing the solutions at each step.
Are you ready? Let’s dive into the questions!
1. Valid Java Array Initialization
Which of the following options is a 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 the answer and explanation.
2. Reserved Keywords in Java
Identify the reserved keywords from the following list:
- A.
array
- B.
goto
- C.
null
- D.
int
Click to reveal the answer and explanation.
3. Analyzing the Output of Interface Constants
interface Foo { int x = 10; }
public class Test {
public static void main(String[] args) {
Foo.x = 20;
System.out.println(Foo.x);
}
}
What will happen if we try to compile and run the above program?
- A. Prints 10
- B. Prints 20
- C. Compile Time Error
- D. Runtime error because
Foo.x
isfinal
.
Click to reveal the answer and explanation.
4. Character Representation in Java
public class Test {
public static void main(String[] args) {
char c = 65;
System.out.println("c = " + c);
}
}
What will be the output of the above program?
- A. Compile Time Error
- B. Prints
c = A
- C. Runtime Error
- D. Prints
c = 65
Click to reveal the answer and explanation.
5. Arithmetic Operations in Java
public class Test {
public void main(String[] args) {
int x = 10 * 20 - 20;
System.out.println(x);
}
}
What will be the result of running this program?
- A. Runtime Error
- B. Prints 180
- C. Prints 0
- D. Compile-time error
Click to reveal the answer and explanation.
6. Static Keyword in Java
Which of the following statements are valid concerning the static
keyword in Java?
- A. We can have a static block in a class.
- B. The static block is executed every time an object of the class is created.
- C. Static methods can be implemented in interfaces.
- D. A static block can be defined inside a method.
Click to reveal the answer and explanation.
7. Core Concepts of OOP (Object-Oriented Programming)
Which of the following are core OOP concepts?
- A. Abstraction
- B. Inheritance
- C. Interface
- D. Polymorphism
- E. Generics
Click to reveal the answer and explanation.
8. True Statements about Inheritance
Consider the following statements about inheritance in Java:
- A. The
extends
keyword is used to extend a class. - B. Multiple class inheritance is possible in Java.
- C. Private members of a superclass are accessible to a subclass.
- D. Final classes cannot be extended.
Click to reveal the answer and explanation.
9. Understanding Method Overriding
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");
}
}
What will be the output of the above program?
- A. Compile time error
- B. Prints “Super”
- C. Prints “Subclass”
- D. Runtime error
Click to reveal the answer and explanation.
Conclusion
This quiz offers a practical way to check and reinforce your Java skills. If you found it helpful, feel free to share it with your colleagues. Also, let me know if you think some essential topics are missing, and I will add more questions to further challenge you!