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
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");
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);
}
}
3. What is the output of the following code snippet?
int[] arr = {1, 2, 3, 4, 5};
System.out.println(arr.length);
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");
}
}
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);
}
}
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--;
}
}
}
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;
}
}
}
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 + " ");
}
}
}
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);
}
}
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);
}
}
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
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); } }
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
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); } }
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);
}
}
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() + " ");
}
}
}
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() + " ");
}
}
}
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();
}
}
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();
}
}
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);
}
}
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();
}
}
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);
}
}
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();
}
}
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();
}
}
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!");
}
}
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);
}
}
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();
}
}
Post Comment