UpdateGadh

UPDATEGADH.COM

Java interview question – Quiz 3

Java interview question

Do you possess the necessary skills to ace a Java interview? We are available to assist you with solidifying your Java skills and ideas. Let’s have a brief understanding of Java before we get started.

What is Java?

The high-level programming language Java was created in 1982 by James Gosling. It may be used to create extensive applications and is founded on the ideas of object-oriented programming.

All the frequently asked Java coding interview questions as well as the most popular Core Java, String Handling, Java 8 and multithreading interview questions, Java OOPs, Java exception handling, and collections interview questions are covered in the article that follows.

Java interview question

Java interview question
Java interview question

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");
a) 15Hello 
b) Hello15 
c) 510Hello 
d) 5Hello10 

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);
         }
    }
a) true 
b) false 
c) It will cause a compilation error. 
d) It will throw a runtime exception. 

3. What is the output of the following code snippet?

int[] arr = {1, 2, 3, 4, 5};
    System.out.println(arr.length);
a) 1 
b) 2 
c) 3 
d) 5 

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");
         }
    }
a) x is less than y 
b) x is greater than y 
c) x is equal to y 
d) The code will not compile due to a syntax error. 

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);
         }
    }
a) 10 
b) 5 
c) 15 
d) The code will not compile due to a syntax error. 

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--;
            }
         }
    }
a) 10 9 8 7 6 5 4 3 2 1 
b) 10 9 8 7 6 5 4 3 2 1 0 
c) 1 2 3 4 5 6 7 8 9 10 
d) The code will not compile due to a syntax error. 

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;
            }
         }
    }
a) 0 1 2 
b) 0 1 2 3 4 
c) 0 1 
d) 0 1 2 3 4 5 

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 + " ");
            }
         }
    }
a) 0 1 2 3 4 
b) 0 1 3 4 
c) 2 
d) 0 1 3 4 5 

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);
         }
    }
a) 0 1 2 3 4 
b) 0 1 2 3 4 5 
c) 1 2 3 4 5 
d) The code will not compile due to a syntax error.

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);
        }
    }
a) 10
b) 11
c) 9
d) Compile-time error

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();
        }
    }
a)
Base
    Derived
    DeriDerived
    
b)
Derived
    DeriDerived
    
c)
DeriDerived
    Derived
    Base
    
d)
DeriDerived
    Derived

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);
        }
    }
a) The program results in a compiler error (“ambiguous overload”).
b) long
c) Integer
d) int

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
    
b)
s1 and s2 equal
    s1 and s3 not equal
    
c)
s1 and s2 not equal
    s1 and s3 equal
    
d)
s1 and s2 not equal
    s1 and s3 not equal

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);
        }
    }
a) false
b) true

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);
        }
    }
a) Java
b) Java Programming
c) Programming
d) Compile-time error

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() + " ");
            }
        }
    }
a) 1 
b) 1234 
c) 123456789 
d) Compile-time error

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() + " ");
            }
        }
    }
a) a b c 
b) A B C 
c) a b c A B C 
d) Compile-time error

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();
        }
    }
a) 2
b) 1
c) Compile-time error
d) Run-time error

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();
        }
    }
a) 2
b) 1
c) Compile-time error
d) Run-time error

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);
        }
    }
a) int constructor
b) long constructor
c) Compile-time error
d) Run-time error

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();
        }
    }
a) Parent
b) Child
c) Compile-time error
d) Run-time error

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);
        }
    }
a) int
b) short
c) Compile-time error
d) Run-time error

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();
        }
    }
a) Demo
b) No Output
c) Compile-time error
d) Run-time error

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();
        }
    }
a) Three
b) One
c) One,Two,Three
d) Run-time error

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!");
        }
    }
a) Hello
b) Hello World!
c) No Output
d) Run-time error

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);
        }
    }
a) Prabhas
b) The name is Ramesh
c) No Output
d) Run-time error

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();
        }
    }
a) Static Method
b) Throws a NullPointerException
c) Compile-time error
d) Runtime error
Interview Question – Updategadh
updategadh.com

Free Projects – Updategadh
updategadh.com

Car Rental Project in PHP and Mysql
Car Rental Project in PHP and Mysql
updategadh.com

Leave a Reply

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