Java is a widely used and versatile programming language known for its platform independence, robustness, and scalability. Whether you are a Java enthusiast, a beginner, or an experienced developer, taking coding quizzes can be a fun and effective way to test your knowledge and learn new concepts.
Check out 100+ quiz questions: 100+ Quiz Questions to Test Your Java, Spring Boot, Microservices, Hibernate, REST API Skills
In this blog post, we present a Java coding quiz with 25+ multiple-choice questions, each accompanied by detailed answers and explanations. Let's get started!
1. What is the output of the following code snippet?
int x = 5;
int y = 10;
System.out.println(x + y + "Hello");
Answer:
Explanation:
2. What is the output of the following code snippet?
public class Main{
public static void main(String []args){
String str1 = "Hello";
String str2 = new String("Hello");
System.out.println(str1 == str2);
}
}
Answer:
Explanation:
3. What is the output of the following code snippet?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr.length);
Answer:
Explanation:
4. What is the output of the following Java program?
public class Main{
public static void main(String []args){
int x = 5;
int y = 10;
if (x < y)
System.out.println("x is less than y");
else if (x > y)
System.out.println("x is greater than y");
else
System.out.println("x is equal to y");
}
}
Answer:
Explanation:
5. What is the output of the following Java program?
public class Main{
public static void main(String []args){
int x = 10;
int y = 5;
int z = (x > y) ? x : y;
System.out.println(z);
}
}
Answer:
Explanation:
6. What is the output of the following Java program?
public class Main{
public static void main(String []args){
int x = 10;
while (x > 0) {
System.out.print(x + " ");
x--;
}
}
}
Answer:
Explanation:
7. What is the output of the following Java program?
public class Main{
public static void main(String []args){
for (int i = 0; i < 5; i++) {
System.out.print(i + " ");
if (i == 2)
break;
}
}
}
Answer:
Explanation:
8. What is the output of the following Java program?
public class Main{
public static void main(String []args){
for (int i = 0; i < 5; i++) {
if (i == 2)
continue;
System.out.print(i + " ");
}
}
}
Answer:
Explanation:
9. What is the output of the following Java program?
public class Main{
public static void main(String []args){
int i = 0;
do {
System.out.print(i + " ");
i++;
} while (i < 5);
}
}
Answer:
Explanation:
10. What is the output of the following program?
public class Main {
public static void main(String[] args) {
int x = 10;
int y = x++;
System.out.println(y);
}
}
Answer:
a) 10
Explanation:
The postfix increment operator (x++) first assigns the value of x to y, and then increments the value of x. Therefore, the value of y is 10.
11. What will be the output of the following Java program?
class Base { public Base() { System.out.println("Base"); } } class Derived extends Base { public Derived() { System.out.println("Derived"); } } class DeriDerived extends Derived { public DeriDerived() { System.out.println("DeriDerived"); } } public class Test { public static void main(String[] args) { Derived b = new DeriDerived(); } }
Base
Derived
DeriDerived
Derived
DeriDerived
DeriDerived
Derived
Base
DeriDerived
Derived
Answer:
Base
Derived
DeriDerived
Explanation:
12. What will be the output of the following Java program?
public class Test { public void print(Integer i) { System.out.println("Integer"); } public void print(int i) { System.out.println("int"); } public void print(long i) { System.out.println("long"); } public static void main(String args[]) { Test test = new Test(); test.print(10); } }
Answer:
Explanation:
13. What will be the output of the following Java program?
public class StrEqual { public static void main(String[] args) { String s1 = "hello"; String s2 = new String("hello"); String s3 = "hello"; if (s1 == s2) { System.out.println("s1 and s2 equal"); } else { System.out.println("s1 and s2 not equal"); } if (s1 == s3) { System.out.println("s1 and s3 equal"); } else { System.out.println("s1 and s3 not equal"); } } }Which one of the following options provides the output of this program when executed?
a)
s1 and s2 equal
s1 and s3 equal
s1 and s2 equal
s1 and s3 not equal
s1 and s2 not equal
s1 and s3 equal
s1 and s2 not equal
s1 and s3 not equal
Answer:
c)s1 and s2 not equal
s1 and s3 equal
Explanation:
14. What is the output of the following Java program?
public class Test { public static void main(String[] args) { String s1 = "hello"; String s2 = new String("hello"); s2 = s2.intern(); System.out.println(s1 == s2); } }
Answer:
Explanation:
15. What is the output of the following code snippet?
public class Main {
public static void main(String[] args) {
String str = "Java";
str.concat(" Programming");
System.out.println(str);
}
}
Answer:
Explanation:
The concat() method returns a new string resulting from concatenating the specified string to the original string. However, in this code, the result of concat() is not assigned back to the str variable. Therefore, the original string "Java" remains unchanged, and the output is "Java".16. What is the output of the following code snippet?
import java.util.regex.*;
public class RegexQuiz {
public static void main(String[] args) {
String regex = "\\d+";
String input = "1234";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.print(matcher.group() + " ");
}
}
}
Answer:
Explanation:
The regex pattern "\\d+" is used, which matches one or more digits. Since the input string "1234" consists of only digits, the regex pattern matches the entire string. Therefore, the output will be "1234".
17. What is the output of the following code snippet?
import java.util.regex.*;
public class RegexQuiz {
public static void main(String[] args) {
String regex = "[a-c]";
String input = "abcABC";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
System.out.print(matcher.group() + " ");
}
}
}
Answer:
Explanation:
The regex "[a-c]" matches any character between 'a' and 'c' (inclusive) in a case-sensitive manner. In the input string "abcABC", it matches "a", "b", and "c" and prints them.
18. What is the output of the following Java program?
class One{
public static void print(){
System.out.println("1");
}
}
class Two extends One{
public static void print(){
System.out.println("2");
}
}
public class Test{
public static void main(String args[]){
One one = new Two();
one.print();
}
}
Answer:
Explanation:
19. What is the output of the following Java program?
class One{
public void print(){
System.out.println("1");
}
}
class Two extends One{
public void print(){
System.out.println("2");
}
}
public class Test{
public static void main(String args[]){
One one = new Two();
one.print();
}
}
Answer:
Explanation:
20. What is the output of the following Java program?
class One{
public One(int x){
System.out.print("int constructor");
}
public One(long l){
System.out.print("long constructor");
}
}
public class Test{
public static void main(String[] args){
long l = 20L;
One one = new One(l);
}
}
Answer:
Explanation:
21. What is the output of the following Java program?
class Parent{
public void className(){
System.out.println("Parent");
}
}
class Child extends Parent{
void className(){
System.out.println("Child");
}
}
public class Test{
public static void main(String[] args){
Parent parent = new Child();
parent.className();
}
}
Answer:
Explanation:
22. What is the output of the following Java program?
class Demo{
public Demo(int i){
System.out.println("int");
}
public void Demo(short s){
System.out.println("short");
}
}
public class Test{
public static void main(String[] args){
short s = 10;
Demo demo = new Demo(s);
}
}
Answer:
Explanation:
23. What is the output of the following Java program?
class Demo{
void Demo(){
System.out.println("Demo");
}
}
public class Test{
public static void main(String[] args){
Demo demo = new Demo();
}
}
Answer:
Explanation:
24. What is the output of the following Java program?
class One{
public One(){
System.out.print("One,");
}
}
class Two extends One{
public Two(){
System.out.print("Two,");
}
}
class Three extends Two{
public Three(){
System.out.print("Three");
}
}
public class Test{
public static void main(String[] args){
Three three = new Three();
}
}
Answer:
Explanation:
25. What is the output of the following Java program?
class Hello{
public Hello(){
System.out.println("Hello");
}
}
public class Main{
Hello hello = new Hello();
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Answer:
Explanation:
26. What is the output of the following Java program?
public class Main{
static String name = "Ramesh";
public Main(){
name = "Prabhas";
}
public static void main(String[] args){
System.out.println("The name is " + name);
}
}
Answer:
Explanation:
27. What will be the output of the following program?
class First
{
static void staticMethod()
{
System.out.println("Static Method");
}
}
public class MainClass
{
public static void main(String[] args)
{
First first = null;
first.staticMethod();
}
}
Comments
Post a Comment
Leave Comment