TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: 30 Java Coding Questions for Programmers
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile Concepts Simplified
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
Java Tutorials

30 Java Coding Questions for Programmers

Last updated: Feb 25, 2024 2:20 am
By Meenakshi Agarwal
Share
18 Min Read
Java Coding Questions to Assess Programming Skills
Java Coding Questions to Assess Programming Skills
SHARE

Are you ready to test your Java coding skills? Here are 30 engaging Java coding questions for programmers designed to assess their programming prowess. These questions cover a range of topics, from basic data structures and algorithms to advanced object-oriented programming concepts. Let’s dive in!

Top 30 Java Coding Questions for Programmers

Programmers love to face challenges like reversing a string without using built-in functions, checking whether a string is a palindrome, and calculating the factorial of a number using recursion. Our Java coding questions for programmers will test your understanding of fundamental programming concepts and the ability to solve problems efficiently.

Head Start with Set-1 (10 Questions )

The set-1 has 10 Java coding questions. To get ready to face it, please prepare with your IDE or use any online IDE to run the given code.

Question-1. Which of the following would the below Java coding snippet return as its output?

class Super {
  public int index = 1;
}

class App extends Super {

  public App(int index) {
    index = index;
  }

  public static void main(String args[]) {
    App myApp = new App(10);
    System.out.println(myApp.index);
  }
}

A. 0
B. 10
C. 1
D. Compile time error

Hover here to view the answer!
Answer. C

Question-2. Which of the following combinations would the below Java coding snippet print?

class TestApp {
  protected int x, y;
}

class Main {
  public static void main(String args[]) {
    TestApp app = new TestApp();
    System.out.println(app.x + " " + app.y);
  }
}

A. 0 1
B. 1 0
C. 0 0
D. null null

Hover here to view the answer!
Answer. C

Question-3. What would be the outcome of the below Java code?

class TestApp {
  public static void main(String[] args) {
    for (int index = 0; 1; index++) {
      System.out.println("Welcome");
      break;
    }
  }
}

A. Welcome
B. Welcome Welcome
C. Type mismatch error
D. Run infinite-times

Hover here to view the answer!
Answer. C

Question-4. What would the below Java coding snippet print?

class TestApp {
  public static void main(String[] args) {
    for (int index = 0; true; index++) {
      System.out.println("Welcome");
      break;
    }
  }
}

A. Welcome
B. None
C. Type mismatch error
D. Run infinite times

Hover here to view the answer!
Answer. A

Question-5. Which of the following values would the below Java coding snippet print in the results?

class TestApp {
  int i[] = {
    0
  };

  public static void main(String args[]) {
    int i[] = {
      1
    };
    alter(i);
    System.out.println(i[0]);
  }

  public static void alter(int i[]) {
    int j[] = {
      2
    };
    i = j;
  }
}

A. 0
B. 1
C. 2
D. Compilation error

Hover here to view the answer!
Answer. B

Question-6. Which of the following is the result of the following Java code?

class TestApp {

  String args[] = {
    "1",
    "2"
  };

  public static void main(String args[]) {
    if (args.length > 0)
      System.out.println(args.length);
  }
}

A. The program compiles but prints nothing.
B. The program fails to compile.
C. The program compiles and prints 2.
D. The program compiles and prints 0.

Hover here to view the answer!
Answer. A

Question-7. What is the result of the following Java coding snippet?

class TestApp {

  public static void main() {
    int odd = 1;
    if (odd) {
      System.out.println("odd");
    } else {
      System.out.println("even");
    }
  }
}

A. odd
B. even
C. Run-time exception
D. Type mismatch error

Hover here to view the answer!
Answer. D

Question-8. What would the following function yield when called?

public void test(boolean a, boolean b) {
  if (a) {
    System.out.println("A");
  } else if (a && b) {
    System.out.println("A && B");
  } else {
    if (!b) {
      System.out.println("!B");
    } else {
      System.out.println("None");
    }
  }
}

A. If a and b both are true, then the output is “A && B”.
B. If a is true and b is false, then the output is “!B”.
C. If a is false and b is true, then the output is “None”.
D. If a and b both are false, then the output is “None”.

Hover here to view the answer!
Answer. C

Question-9. What would the following Java coding snippet return as its output?

class TestApp {

  public static void main(String[] args) {
    class Tutorial {
      public String name;

      public Tutorial(String tutorial) {
        name = tutorial;
      }
    }

    Object obj = new Tutorial("Java Quiz");
    Tutorial tutorial = (Tutorial) obj;
    System.out.println(tutorial.name);
  }
}

A. An exception occurs while instantiating the Tutorial class.
B. It’ll print “Java Quiz”.
C. The program will print null.
D. Compilation error at line number 13.

Hover here to view the answer!
Answer. B

Question-10. What does the following Java coding snippet print?

import java.io.CharArrayReader;
import java.io.IOException;

class TestApp {

  public static void main(String[] args) {
    String obj = "abcdef";
    int length = obj.length();
    char c[] = new char[length];
    obj.getChars(0, length, c, 0);
    CharArrayReader io_1 = new CharArrayReader(c);
    CharArrayReader io_2 = new CharArrayReader(c, 0, 3);
    int i;
    try {
      while ((i = io_1.read()) != -1) {
        System.out.print((char) i);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

A. abc
B. abcd
C. abcde
D. abcdef

Hover here to view the answer!
Answer. D

Set-2 of Java Coding Questions for Programmers.

Before getting ahead with the next set of Java coding questions, would you like to share your experience? If yes, please use our contact page and let us know. If you want us to come up with more resources, email us including your suggestions.

Question-11. What will be the output of the following Java coding snippet?

import java.io.CharArrayReader;
import java.io.IOException;

class TestApp {

  public static void main(String[] args) {
    String obj = "abcdef";
    int length = obj.length();
    char c[] = new char[length];
    obj.getChars(0, length, c, 0);
    CharArrayReader io_1 = new CharArrayReader(c);
    CharArrayReader io_2 = new CharArrayReader(c, 0, 3);
    int i;
    try {
      while ((i = io_2.read()) != -1) {
        System.out.print((char) i);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

A. abc
B. abcd
C. abcde
D. abcdef

Hover here to view the answer!
Answer. A

Question-12. What would the following Java coding snippet return?

import java.io.CharArrayReader;
import java.io.IOException;

class TestApp {

  public static void main(String[] args) {
    String obj = "abcdef";
    int length = obj.length();
    char c[] = new char[length];
    obj.getChars(0, length, c, 0);
    CharArrayReader io_1 = new CharArrayReader(c);
    CharArrayReader io_2 = new CharArrayReader(c, 1, 4);
    int i, j;
    try {
      while ((i = io_1.read()) == (j = io_2.read())) {
        System.out.print((char) i);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

A. abc
B. abcd
C. abcde
D. abcdef
E. Nothing would get printed.

Hover here to view the answer!
Answer. E

Note- Nothing would get printed. Since none of the char in the arrays matches, so the control would come out of the loop without printing anything.

Question-13. What is the outcome of the below Java code?

class TestApp {

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

  static float test() {
    static float x = 0.0;
    return ++x;
  }
}

A. 0.0
B. 1
C. 1.0
D. Compile time error

Hover here to view the answer!
Answer. D

Note- The program would result in a compile error. Unlike C++, Java doesn’t support static variables declared as local. Though, a class can have static members to compute the number of function calls or other purposes.

Question-14. What does the following Java coding snippet yield?

class TestApp {

  static int index = 0;

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

  int test() {
    int index = 1;
    return index;
  }
}

A. 0
B. 1
C. Run-time error at line number 6
D. Compile time error

Hover here to view the answer!
Answer. D

Note- In Java, non-static methods aren’t allowed to get called from a static method. If we turn test() to static, then the program will compile without any compiler error.

When it comes to object-oriented programming (OOP) concepts, be prepared to demonstrate your knowledge of inheritance, polymorphism, and encapsulation. Our Java coding questions for programmers not only assess your technical skills but also your ability to think critically and logically. So grab your favorite IDE, and warm up your coding fingers.

Question-15. Which of the following is the result of the below Java coding snippet?

class TestApp {

  public static void main(String args[]) {
    int bits;

    bits = -3 >> 1;
    bits = bits >>> 2;
    bits = bits << 1;
    System.out.println(bits);
  }
}

A. 1
B. 7
C. -2147483646
D. 2147483646

Hover here to view the answer!
Answer. D

Question-16. Which of the following is a result of the Java code given below?

class TestApp {

  public static void main(String args[]) {
    int index = 0;
    boolean flag = true;
    boolean reg1 = false, reg2;
    reg2 = (flag | ((index++) == 0));
    reg2 = (reg1 | ((index += 2) > 0));

    System.out.println(index);
  }
}

A. 0
B. 1
C. 2
D. 3

Hover here to view the answer!
Answer. D

Question-17. What would the following Java coding snippet display on execution?

// Command - line: java TestApp 1 2 3 4 5

class TestApp {

  public static void main(String[] args) {

    System.out.println(args[1] + args[2] + args[3]);
  }
}

A. 1 2 3
B. 123
C. 234
D. Compilation Error

Hover here to view the answer!
Answer. C

Question-18. What would the below Java coding snippet print if the input given is?

// Command - line: java TestApp abcqfghqbcd

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class TestApp {

  public static void main(String args[]) throws IOException {
    char bit;
    BufferedReader obj = new BufferedReader(new InputStreamReader(System.in));
    do {
      bit = (char) obj.read();
      System.out.print(bit);
    } while (bit != 'q');
  }
}

A. abcqfgh
B. abc
C. abcq
D. abcqfghq

Hover here to view the answer!
Answer. C

Question-19. What would the following Java coding snippet yield on execution?

import java.io.File;

class TestApp {

  public static void main(String args[]) {
    File sys = new File("/java/system");
    System.out.print(sys.canWrite());
    System.out.print(" " + sys.canRead());
  }
}

A. true false
B. false true
C. true true
D. false false

Hover here to view the answer!
Answer. D

Question-20. What does the following Java coding snippet print as its output?

class Cluster {}

class Node1 extends Cluster {}

class Node2 extends Cluster {}

public class TestApp {
  public static void main(String[] args) {
    Cluster tree = new Node1();
    if (tree instanceof Node1)
      System.out.println("Node1");
    else if (tree instanceof Cluster)
      System.out.println("Cluster");
    else if (tree instanceof Node2)
      System.out.println("Node2");
    else
      System.out.println("Unexpected");
  }
}

A. Cluster
B. Node1
C. Node2
D. Unexpected

Hover here to view the answer!
Answer. B

Stay tuned to access the next set of 10 more Java coding questions for programmers…

10 More Java Coding Questions for Programmers

Believe it or not, only practice can make a programmer the best. Search for coding problems, and pay attention to solving them. Rethink, and rewrite better code, and you will reach a level that you wish to achieve.

Question-21. Which of the following is the result of the below program?

public class SimpleTest {
  public static void stringReplace(String str) {
    str = str.replace('c', 'c');
  }

  public static void bufferReplace(StringBuffer str) {
    str.trimToSize();
  }

  public static void main(String args[]) {
    String myString = new String("cplus");
    StringBuffer myBuffer = new StringBuffer(" plus");
    stringReplace(myString);
    bufferReplace(myBuffer);
    System.out.println(myString + myBuffer);
  }
}

A. cplusplus
B. plus plus
C. cplus plus
D. c plus plus

Hover here to view the answer!
Answer. C

Question-22. Which of the following is the outcome of the below program? Assume the given input is <abc’def/’egh>.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class SimpleTest {

  public static void main(String args[]) throws IOException {
    char bit;
    BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
    do {
      bit = (char) console.read();
      System.out.print(bit);
    } while (bit != '\'');
  }
}

A. abc’
B. abcdef/’
C. abc’def/’egh
D. abcqfghq

Hover here to view the answer!
Answer. A

Question-23. Which of the following is the result of the below Java coding snippet?

import java.io.File;

public class SimpleTest {

  public static void main(String args[]) {
    File sys = new File("/MVC/system");
    System.out.print(sys.getParent());
    System.out.print(" " + sys.isFile());
  }
}

A. MVC true
B. MVC false
C. \MVC false
D. \MVC true

Hover here to view the answer!
Answer. C

Question-24. Which of the following would the below Java coding snippet return on execution?

public class SimpleTest {

  static int test;
  boolean final() {
    test++;
    return true;
  }

  public static void main(String[] args) {
    test = 0;
    if ((final() | final()) || final())
      test++;
    System.out.println(test);
  }
}

A. 1
B. 2
C. 3
D. Compilation error

Hover here to view the answer!
Answer. D

Question-25. Which of the following values would the below Java coding snippet yield?

public class SimpleTest {

  public static void main(String[] args) {
    String text = "199";
    try {
      text = text.concat(".5");
      double decimal = Double.parseDouble(text);
      text = Double.toString(decimal);
      int status = (int) Math.ceil(Double.valueOf(text).doubleValue());
      System.out.println(status);
    } catch (NumberFormatException e) {
      System.out.println("Invalid number");
    }
  }
}

A. 199
B. 199.5
C. 200
D. Invalid number

Hover here to view the answer!
Answer. C

Question-26. Which of the following combinations would the below program print?

public class SimpleTest {

  public static void main(String ags[]) {
    String initial = "ABCDEFG", after = "";
    after = initial = initial.replace('A', 'Z');
    System.out.println(initial + ", " + after);
  }
}

A. ABCDEFG, ABCDEFG
B. ABCDEFG, ZBCDEFG
C. ZBCDEFG, ABCDEFG
D. ZBCDEFG, ZBCDEFG

Hover here to view the answer!
Answer. D

Question-27. Which of the following values would the below Java coding snippet print?

public class SimpleTest {

  public static void main(String args[]) {
    String str = (String) returnStringAsArray()[-1 + 1 * 2];
    System.out.println(str);
  }

  public static Object[] returnStringAsArray() {
    return new String[] {
      "Java",
      "Quiz"
    };
  }
}

A. Java
B. ArrayIndexOutOfBoundsException
C. Quiz
D. Compilation error

Hover here to view the answer!
Answer. C

Question-28. What would the below Java coding snippet print on execution?

public class SimpleTest {

  public static void main(String args[]) {
    try {
      args[0] = "0";
      return;

    } catch (Exception e) {
      System.out.print("Exception");
    } finally {
      System.out.print("Final");
    }
  }
}

A. Exception
B. Final
C. ExceptionFinal
D. Compilation error

Hover here to view the answer!
Answer. C

Question-29. What does the following Java coding snippet print on execution?

public class SimpleTest {

  public static void main(String[] args) {
    int[] table = {
      1,
      2,
      3,
      4,
      5
    };
    table[1] = (table[2 * 1] == 2 - args.length) ? table[3] : 99;
    System.out.println(table[1]);
  }
}

A. Compilation fails.
B. 3
C. 2
D. 99

Hover here to view the answer!
Answer. D

Question-30. What would be the output of the below Java coding snippet upon execution?

import java.util.Random;

public class SimpleTest {

  static int count = 0;

  public static void main(String[] args) throws InterruptedException {
    Consumer test = new Consumer();
    Producer prod1 = new Producer(test, "thread-1");
    Producer prod2 = new Producer(test, "thread-2");
    prod1.start();
    prod2.start();
  }
}

class Producer extends Thread {
  Consumer test;
  String message;

  Producer(Consumer test, String msg) {
    this.test = test;
    message = msg;
  }

  public void run() {
    Random rand = new Random();
    int randomNum = rand.nextInt((1000 - 10) + 1) + 10;
    System.out.println(message);
  }
}

class Consumer {
  private int count = 0;

  public int nextCounter() {
    synchronized(this) {
      count++;
      return count;
    }
  }
}

A. Runtime Exception
B. thread-1 thread-2
C. thread-2 thread-1
D. Sometimes thread-2 will precede thread-1.

Hover here to view the answer!
Answer. D

Conclusion

We trust that you have done a lot of Java programming practice with the help of the 30 most challenging Java questions. However, don’t leave without checking another set of 10 Java coding questions for testers.

Keep Practicing Java and Brace for a New Java Coding Challenge!

All the Best,

TechBeamers

You Might Also Like

A Simple Guide to Exception Handling in Java

Difference Between Spring and Spring Boot

How to Use Java String Format with Examples

Java IRC Bot with Sample Code

Generate Random Number in Java – 10 Ways

TAGGED:Java Coding TipsJava Questions Answers
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article Basic Linux Questions and Answers - Online Test for Freshers Basic Linux Questions and Answers
Next Article Must Know Linux Interview Questions for Job Aspirants 20 Must-Know Linux Interview Questions

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x