Breaking Posts

6/trending/recent
Type Here to Get Search Results !

MCQ (Multiple Choice Question) On JAVA Programming

This is the photo of MCQ questions fill by student in OMR sheet & Here Java programming MCQ were given.

(Image: Istockphoto)

Here some multiple-choice questions and answers are given of Java Programming language.  Java Programming MCQ (Multiple Choice Questions) is a useful tool for evaluating one's understanding of the Java language. MCQ on Java includes a set of questions with multiple options and the correct answer(s) to choose from. These question answers on Java can be utilized by teachers to assess their students' knowledge or by job recruiters to evaluate the proficiency of job applicants. With the increasing demand for Java programming skills in the IT industry, mastering Java Programming MCQs is crucial for anyone who wants to succeed in this field.

Java Programming MCQ covers a wide range of topics, including data types, control statements, arrays, classes, inheritance, exception handling, and more. These questions are designed to test not only the basic knowledge of Java programming but also the ability to apply it to solve real-world problems. By practicing MCQs on Java, learners can not only identify their strengths and weaknesses but also gain the confidence and skills necessary to tackle complex programming tasks. Moreover, MCQ on Java is an efficient and time-saving method of assessing one's knowledge as compared to traditional testing methods. Therefore, whether you are a beginner or an experienced programmer, mastering Java Programming MCQs can enhance your career prospects and boost your overall programming skills.

Multiple Choice Question (MCQ) On Java

Which of the following is not a primitive data type in Java?

a. int

b. boolean

c. double

d. string

Answer: d. string


Which of the following is a valid identifier in Java?

a. 123abc

b. _abc123

c. #abc123

d. *abc123

Answer: b. _abc123


What is the output of the following code snippet?

int a = 5;

int b = 10;

System.out.println("a + b = " + a + b);

a. a + b = 15

b. a + b = 510

c. Error

d. None of the above

Answer: b. a + b = 510


Which of the following is true about inheritance in Java?

a. A class can extend only one class at a time

b. A subclass can access private members of its superclass

c. The final keyword can be used to prevent a class from being inherited

d. All of the above

Answer: a. A class can extend only one class at a time


What is the output of the following code snippet?

int x = 10;

if (x > 5 && x < 15) {

System.out.println("x is between 5 and 15");

}

a. x is between 5 and 15

b. x is greater than 5

c. x is less than 15

d. None of the above

Answer: a. x is between 5 and 15


What is the difference between ArrayList and LinkedList in Java?

a. ArrayList is a linked list implementation, while LinkedList is an array implementation.

b. ArrayList is faster than LinkedList for inserting and deleting elements.

c. LinkedList is faster than ArrayList for accessing elements by index.

d. None of the above

Answer: c. LinkedList is faster than ArrayList for accessing elements by index.


What is the purpose of the "finally" block in Java try-catch-finally statements?

a. To catch exceptions

b. To execute code that always runs, regardless of whether an exception is thrown or not

c. To handle exceptions

d. None of the above

Answer: b. To execute code that always runs, regardless of whether an exception is thrown or not


Which of the following is true about abstract classes in Java?

a. An abstract class can be instantiated.

b. An abstract class can have non-abstract methods.

c. A class can extend multiple abstract classes.

d. None of the above

Answer: b. An abstract class can have non-abstract methods.


What is the output of the following code snippet?

int[] arr = {1, 2, 3};

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);

}

a. 1 2 3

b. [1, 2, 3]

c. Error

d. None of the above

Answer: a. 1 2 3


Which of the following is true about interfaces in Java?

a. An interface can have instance variables.

b. An interface can have constructors.

c. A class can implement multiple interfaces.

d. None of the above

Answer: c. A class can implement multiple interfaces.


What is the output of the following code snippet?

String str1 = "Hello";

String str2 = "Hello";

System.out.println(str1 == str2);

a. true

b. false

c. Error

d. None of the above

Answer: a. true


What is the difference between method overloading and method overriding in Java?

a. Method overloading occurs when two methods in the same class have the same name but different parameters, while method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.

b. Method overloading occurs when a subclass provides a specific implementation for a method that is already defined in its superclass, while method overriding occurs when two methods in the same class have the same name but different parameters.

c. Method overloading and method overriding are the same thing.

d. None of the above

Answer: a. Method overloading occurs when two methods in the same class have the same name but different parameters, while method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass.


What is the purpose of the "static" keyword in Java?

a. To indicate that a method or variable belongs to the class, rather than to an instance of the class.

b. To indicate that a method or variable can only be accessed within the class.

c. To indicate that a method or variable is final and cannot be changed.

d. None of the above

Answer: a. To indicate that a method or variable belongs to the class, rather than to an instance of the class.


What is the output of the following code snippet?

int i = 0;

while (i < 5) {

System.out.println(i);

i++;

}

a. 0 1 2 3 4

b. 1 2 3 4 5

c. Error

d. None of the above

Answer: a. 0 1 2 3 4


What is the purpose of the "this" keyword in Java?

a. To refer to the current instance of the class.

b. To refer to the superclass of the class.

c. To refer to the subclass of the class.

d. None of the above

Answer: a. To refer to the current instance of the class.


What is the difference between a checked exception and an unchecked exception in Java?

a. A checked exception is a subclass of RuntimeException, while an unchecked exception is any other subclass of Exception.

b. A checked exception must be handled or declared in the method signature, while an unchecked exception does not have to be handled or declared.

c. A checked exception can be caught by a catch block, while an unchecked exception cannot be caught.

d. None of the above

Answer: b. A checked exception must be handled or declared in the method signature, while an unchecked exception does not have to be handled or declared.


What is the output of the following code snippet?

String str = "hello";

str.toUpperCase();

System.out.println(str);

a. hello

b. HELLO

c. Error

d. None of the above

Answer: a. hello


What is the purpose of the "super" keyword in Java?

a. To refer to the current instance of the class.

b. To refer to the superclass of the class.

c. To refer to the subclass of the class.

d. None of the above

Answer: b. To refer to the superclass of the class.


What is the output of the following code snippet?

for (int i = 1; i <= 10; i++) {

if (i % 2 == 0) {

continue;

}

System.out.println(i);

}

a. 1 2 3 4 5 6 7 8 9 10

b. 1 3 5 7 9

c. Error

d. None of the above

Answer: b. 1 3 5 7 9


What is the difference between a constructor and a method in Java?

a. A constructor has a return type, while a method does not.

b. A constructor can be called explicitly, while a method is called implicitly.

c. A constructor has the same name as the class, while a method can have any name.

d. None of the above

Answer: c. A constructor has the same name as the class, while a method can have any name.


What is the output of the following code snippet?

int x = 5;

int y = 10;

if (x > 5 && y < 15) {

System.out.println("true");

} else {

System.out.println("false");

}

a. true

b. false

c. Error

d. None of the above

Answer: b. false


What is the purpose of the "final" keyword in Java?

a. To indicate that a variable cannot be changed.

b. To indicate that a method cannot be overridden.

c. To indicate that a class cannot be subclassed.

d. All of the above

Answer: d. All of the above


What is the output of the following code snippet?

int[] arr = {1, 2, 3, 4, 5};

for (int i : arr) {

System.out.println(i);

}

a. 1 2 3 4 5

b. 5 4 3 2 1

c. Error

d. None of the above

Answer: a. 1 2 3 4 5


What is the purpose of the "break" keyword in Java?

a. To exit a loop or switch statement.

b. To continue to the next iteration of a loop.

c. To throw an exception.

d. None of the above

Answer: a. To exit a loop or switch statement.


What is the difference between a public and private access modifier in Java?

a. A public method or variable can be accessed from any class, while a private method or variable can only be accessed from within the same class.

b. A public method or variable can only be accessed from within the same class, while a private method or variable can be accessed from any class.

c. A public method or variable is final and cannot be changed, while a private method or variable can be changed.

d. None of the above

Answer: a. A public method or variable can be accessed from any class, while a private method or variable can only be accessed from within the same class.


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "world";

String s3 = s1 + s2;

System.out.println(s3.length());

a. 5

b. 10

c. 11

d. None of the above

Answer: c. 11


What is a static method in Java?

a. A method that can only be called from within the same class.

b. A method that can be called without an instance of the class.

c. A method that is final and cannot be overridden.

d. None of the above

Answer: b. A method that can be called without an instance of the class.


What is the output of the following code snippet?

int x = 10;

int y = 5;

System.out.println(x % y);

a. 2

b. 5

c. 10

d. None of the above

Answer: a. 2


What is the purpose of the "this" keyword in Java?

a. To refer to the current instance of the class.

b. To refer to the superclass of the class.

c. To refer to a subclass of the class.

d. None of the above

Answer: a. To refer to the current instance of the class.


What is a package in Java?

a. A group of related classes and interfaces.

b. A group of related variables and methods.

c. A group of related keywords.

d. None of the above

Answer: a. A group of related classes and interfaces.


What is the output of the following code snippet?

int[] arr = {1, 2, 3};

int sum = 0;

for (int i = 0; i < arr.length; i++) {

sum += arr[i];

}

System.out.println(sum);

a. 1

b. 2

c. 3

d. 6

Answer: d. 6


What is the difference between an abstract class and an interface in Java?

a. An abstract class can have both concrete and abstract methods, while an interface can only have abstract methods.

b. An interface can have both concrete and abstract methods, while an abstract class can only have abstract methods.

c. An abstract class can be instantiated, while an interface cannot.

d. None of the above

Answer: a. An abstract class can have both concrete and abstract methods, while an interface can only have abstract methods.


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "world";

System.out.println(s1.compareTo(s2));

a. -5

b. -1

c. 1

d. 5

Answer: b. -1


What is the purpose of the "super" keyword in Java?

a. To refer to the current instance of the class.

b. To refer to the superclass of the class.

c. To refer to a subclass of the class.

d. None of the above

Answer: b. To refer to the superclass of the class.


What is the output of the following code snippet?

int x = 10;

int y = 20;

int z = x > y ? x : y;

System.out.println(z);

a. 10

b. 20

c. 30

d. None of the above

Answer: b. 20


What is the difference between a stack and a queue?

a. A stack is a first-in-first-out (FIFO) data structure, while a queue is a last-in-first-out (LIFO) data structure.

b. A stack is a last-in-first-out (LIFO) data structure, while a queue is a first-in-first-out (FIFO) data structure.

c. A stack and a queue are the same data structure.

d. None of the above

Answer: b. A stack is a last-in-first-out (LIFO) data structure, while a queue is a first-in-first-out (FIFO) data structure.


What is the output of the following code snippet?

int x = 5;

int y = 7;

boolean result = x < y && y > 10;

System.out.println(result);

a. true

b. false

c. compile error

d. runtime error

Answer: b. false


What is a constructor in Java?

a. A method that is used to create objects of a class.

b. A method that is used to destroy objects of a class.

c. A method that is used to modify objects of a class.

d. None of the above

Answer: a. A method that is used to create objects of a class.


What is the output of the following code snippet?

int i = 0;

do {

System.out.println(i);

i++;

} while (i < 5);

a. 0 1 2 3 4

b. 1 2 3 4 5

c. 0 1 2 3 4 5

d. None of the above

Answer: a. 0 1 2 3 4


What is the output of the following code snippet?

String s = "hello world";

System.out.println(s.substring(0, 5));

a. "hello"

b. "world"

c. "hello "

d. None of the above

Answer: a. "hello"


Which of the following is not a valid access specifier in Java?

a. public

b. private

c. protected

d. package

Answer: d. package


What is the output of the following code snippet?

int[] arr = {1, 2, 3};

for (int i : arr) {

System.out.println(i);

}

a. 1 2 3

b. 3 2 1

c. 0 1 2

d. None of the above

Answer: a. 1 2 3


What is a static method in Java?

a. A method that is called on an object of a class.

b. A method that is called on a class itself, rather than on an object of the class.

c. A method that is called on an object of a subclass.

d. None of the above

Answer: b. A method that is called on a class itself, rather than on an object of the class.


What is the output of the following code snippet?

int x = 5;

switch (x) {

case 1:

System.out.println("one");

break;

case 2:

System.out.println("two");

break;

default:

System.out.println("default");

}

a. "one"

b. "two"

c. "default"

d. None of the above

Answer: c. "default"


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "world";

String s3 = s1 + " " + s2;

System.out.println(s3.toUpperCase());

a. "HELLO WORLD"

b. "hello world"

c. "Hello World"

d. None of the above

Answer: a. "HELLO WORLD"


What is the difference between an abstract class and an interface in Java?

a. An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.

b. An abstract class can be instantiated, while an interface cannot.

c. A class can implement multiple interfaces, but can only extend one abstract class.

d. All of the above.

Answer: a. An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.


What is the output of the following code snippet?

int i = 5;

System.out.println(i++);

System.out.println(i);

a. 5 6

b. 6 5

c. 5 5

d. 6 6

Answer: a. 5 6


What is a package in Java?

a. A group of related classes and interfaces.

b. A collection of methods.

c. A type of access specifier.

d. None of the above.

Answer: a. A group of related classes and interfaces.


What is the output of the following code snippet?

int i = 0;

while (i < 5) {

System.out.println(i);

i += 2;

}

a. 0 1 2 3 4

b. 0 2 4

c. 2 4 6 8 10

d. None of the above

Answer: b. 0 2 4


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "hello";

System.out.println(s1 == s2);

a. true

b. false

c. compile error

d. runtime error

Answer: a. true (This is because the strings are the same object in memory, due to String pool optimization.)


What is a constructor in Java?

a. A method that returns a value.

b. A method that is used to create objects.

c. A method that is called on a class itself.

d. A method that is used to modify objects.

Answer: b. A method that is used to create objects.


What is the output of the following code snippet?

int[] arr = {1, 2, 3};

for (int i = 0; i < arr.length; i++) {

System.out.println(arr[i]);

}

a. 1 2 3

b. 3 2 1

c. 0 1 2

d. None of the above

Answer: a. 1 2 3


What is the purpose of the "this" keyword in Java?

a. To refer to the current object.

b. To create a new object.

c. To access a static variable.

d. None of the above.

Answer: a. To refer to the current object.


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "world";

String s3 = s1.concat(s2);

System.out.println(s3);

a. "helloworld"

b. "hello world"

c. "Hello World"

d. None of the above

Answer: a. "helloworld"


What is the output of the following code snippet?

int x = 10;

if (x > 5 && x < 15) {

System.out.println("true");

} else {

System.out.println("false");

}

a. "true"

b. "false"

c. "true false"

d. None of the above

Answer: a. "true"


What is the difference between a public method and a private method in Java?

a. A public method can be accessed from any class, while a private method can only be accessed within the same class.

b. A private method can be accessed from any class, while a public method can only be accessed within the same class.

c. A public method has a return type, while a private method does not.

d. A private method is used to create objects, while a public method is used to modify objects.

Answer: a. A public method can be accessed from any class, while a private method can only be accessed within the same class.


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "world";

String s3 = s1 + s2;

System.out.println(s3);

a. "helloworld"

b. "hello world"

c. "Hello World"

d. None of the above

Answer: a. "helloworld"


What is a static method in Java?

a. A method that is called on an object.

b. A method that is called on a class itself.

c. A method that is used to create objects.

d. A method that is used to modify objects.

Answer: b. A method that is called on a class itself.


What is the output of the following code snippet?

int[] arr = {1, 2, 3};

for (int i : arr) {

System.out.println(i);

}

a. 1 2 3

b. 3 2 1

c. 0 1 2

d. None of the above

Answer: a. 1 2 3


What is the purpose of the "super" keyword in Java?

a. To refer to the current object.

b. To create a new object.

c. To access a static variable.

d. To call a method or constructor in the superclass.

Answer: d. To call a method or constructor in the superclass.


What is the difference between a class and an object in Java?

a. A class is a template or blueprint for creating objects, while an object is an instance of a class.

b. A class is an instance of an object, while an object is a template or blueprint for creating classes.

c. A class and an object are the same thing in Java.

d. None of the above.

Answer: a. A class is a template or blueprint for creating objects, while an object is an instance of a class.


What is the output of the following code snippet?

int x = 5;

switch (x) {

case 1:

System.out.println("one");

break;

case 2:

System.out.println("two");

break;

case 5:

System.out.println("five");

break;

default:

System.out.println("default");

}

a. "one"

b. "two"

c. "five"

d. None of the above

Answer: c. "five"


What is the difference between a while loop and a do-while loop in Java?

a. A while loop is executed at least once, while a do-while loop may not be executed at all.

b. A while loop executes its body only if the condition is true, while a do-while loop executes its body at least once, regardless of the condition.

c. A while loop and a do-while loop are the same thing in Java.

d. None of the above.

Answer: b. A while loop executes its body only if the condition is true, while a do-while loop executes its body at least once, regardless of the condition.


What is the output of the following code snippet?

int x = 10;

int y = x++;

System.out.println(x + " " + y);

a. "11 10"

b. "10 11"

c. "20 10"

d. None of the above

Answer: a. "11 10"


What is the purpose of the "final" keyword in Java?

a. To indicate that a variable cannot be changed once it has been assigned a value.

b. To indicate that a method cannot be overridden in a subclass.

c. To indicate that a class cannot be extended.

d. All of the above.

Answer: d. All of the above.


What is the difference between an interface and a class in Java?

a. An interface can have both abstract and non-abstract methods, while a class can only have non-abstract methods.

b. An interface can only have public static final variables, while a class can have any type of variables.

c. An interface can be implemented by multiple classes, while a class can only extend one class.

d. All of the above.

Answer: c. An interface can be implemented by multiple classes, while a class can only extend one class.


What is the output of the following code snippet?

int i = 0;

do {

System.out.println(i);

i++;

} while (i < 5);

a. "0 1 2 3 4"

b. "1 2 3 4 5"

c. "5 4 3 2 1"

d. None of the above

Answer: a. "0 1 2 3 4"


What is the difference between a break statement and a continue statement in Java?

a. A break statement terminates the loop and jumps to the next statement outside the loop, while a continue statement skips the current iteration of the loop and jumps to the next iteration.

b. A break statement skips the current iteration of the loop and jumps to the next iteration, while a continue statement terminates the loop and jumps to the next statement outside the loop.

c. A break statement and a continue statement are the same thing in Java.

d. None of the above.

Answer: a. A break statement terminates the loop and jumps to the next statement outside the loop, while a continue statement skips the current iteration of the loop and jumps to the next iteration.


What is the output of the following code snippet?

int x = 5;

int y = 3;

System.out.println(x > y ? "x is greater" : "y is greater");

a. "x is greater"

b. "y is greater"

c. "true"

d. None of the above

Answer: a. "x is greater"


What is the purpose of the "this" keyword in Java?

a. To refer to the current object.

b. To create a new object.

c. To access a static variable.

d. To call a method or constructor in the superclass.

Answer: a. To refer to the current object.


What is the difference between a checked exception and an unchecked exception in Java?

a. A checked exception is a runtime error, while an unchecked exception is a compile-time error.

b. A checked exception is a type of exception that must be caught or thrown, while an unchecked exception is not required to be caught or thrown.

c. A checked exception is a subclass of RuntimeException, while an unchecked exception is a subclass of Exception.

d. None of the above.

Answer: b. A checked exception is a type of exception that must be caught or thrown, while an unchecked exception is not required to be caught or thrown.


What is the output of the following code snippet?

int[] arr = {1, 2, 3, 4, 5};

for (int i : arr) {

if (i == 3) {

continue;

}

System.out.println(i);

}

a. "1 2 4 5"

b. "1 2 3 4 5"

c. "1 2 4"

d. None of the above

Answer: a. "1 2 4 5"


What is the difference between an abstract class and an interface in Java?

a. An abstract class can have both abstract and non-abstract methods, while an interface can only have abstract methods.

b. An abstract class can have constructor, while an interface cannot have a constructor.

c. An abstract class can have instance variables, while an interface cannot have instance variables.

d. All of the above.

Answer: d. All of the above.


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "hello";

System.out.println(s1 == s2);

a. true

b. false

c. Compile-time error

d. None of the above

Answer: a. true


What is the purpose of the "super" keyword in Java?

a. To refer to the current object.

b. To create a new object.

c. To access a static variable.

d. To call a method or constructor in the superclass.

Answer: d. To call a method or constructor in the superclass.


What is the difference between the "equals" method and the "==" operator in Java?

a. The "equals" method compares the values of two objects, while the "==" operator compares the memory addresses of two objects.

b. The "equals" method compares the memory addresses of two objects, while the "==" operator compares the values of two objects.

c. The "equals" method is used for comparing primitive data types, while the "==" operator is used for comparing objects.

d. None of the above.

Answer: a. The "equals" method compares the values of two objects, while the "==" operator compares the memory addresses of two objects.


What is the output of the following code snippet?

int i = 0;

while (i < 5) {

System.out.println(i);

i++;

}

a. "0 1 2 3 4"

b. "1 2 3 4 5"

c. "5 4 3 2 1"

d. None of the above

Answer: a. "0 1 2 3 4"


What is the difference between a public variable and a private variable in Java?

a. A public variable can only be accessed within the same class, while a private variable can be accessed by any class in the same package.

b. A public variable can be accessed by any class, while a private variable can only be accessed within the same class.

c. A public variable and a private variable are the same thing in Java.

d. None of the above.

Answer: b. A public variable can be accessed by any class, while a private variable can only be accessed within the same class.


What is the output of the following code snippet?

int x = 5;

int y = 3;

if (x > y) {

System.out.println("x is greater");

} else {

System.out.println("y is greater");

}

a. "x is greater"

b. "y is greater"

c. "true"

d. None of the above

Answer: a. "x is greater"


What is polymorphism in Java?

a. The ability of an object to take on many forms.

b. The ability of a method to return multiple types of values.

c. The ability of a variable to hold multiple values.

d. None of the above.

Answer: a. The ability of an object to take on many forms.


What is a static method in Java?

a. A method that can be called on an object of a class.

b. A method that can only be called within the same package.

c. A method that can be called on the class itself, rather than on an instance of the class.

d. None of the above.

Answer: c. A method that can be called on the class itself, rather than on an instance of the class.


What is the output of the following code snippet?

int i = 5;

switch (i) {

case 1:

System.out.println("One");

break;

case 2:

System.out.println("Two");

break;

case 3:

System.out.println("Three");

break;

default:

System.out.println("Other");

break;

}

a. "One"

b. "Two"

c. "Three"

d. "Other"

Answer: d. "Other"


What is a constructor in Java?

a. A method that is used to create a new object of a class.

b. A method that is used to destroy an object of a class.

c. A method that is used to access a private variable.

d. None of the above.

Answer: a. A method that is used to create a new object of a class.


What is the difference between an array and an ArrayList in Java?

a. An array is a fixed-size data structure, while an ArrayList can grow or shrink dynamically.

b. An array can only store primitive data types, while an ArrayList can store any object.

c. An array is an object in Java, while an ArrayList is a primitive data type.

d. None of the above.

Answer: a. An array is a fixed-size data structure, while an ArrayList can grow or shrink dynamically.


What is the purpose of the "this" keyword in Java?

a. To refer to the current object.

b. To create a new object.

c. To access a static variable.

d. To call a method or constructor in the superclass.

Answer: a. To refer to the current object.


What is the output of the following code snippet?

String str = "Hello";

str += " World";

System.out.println(str);

a. "Hello World"

b. "World Hello"

c. "Hello"

d. None of the above

Answer: a. "Hello World"


What is an interface in Java?

a. An interface is a blueprint for a class that specifies a set of methods that must be implemented by any class that implements the interface.

b. An interface is a data structure that holds a collection of key-value pairs.

c. An interface is a way to restrict access to a method or variable.

d. None of the above.

Answer: a. An interface is a blueprint for a class that specifies a set of methods that must be implemented by any class that implements the interface.


What is the difference between a checked exception and an unchecked exception in Java?

a. A checked exception is an exception that is checked at compile-time, while an unchecked exception is an exception that is checked at runtime.

b. A checked exception is an exception that is checked at runtime, while an unchecked exception is an exception that is checked at compile-time.

c. A checked exception must be caught or declared in the method signature, while an unchecked exception does not have to be caught or declared.

d. None of the above.

Answer: c. A checked exception must be caught or declared in the method signature, while an unchecked exception does not have to be caught or declared.


What is the output of the following code snippet?

int[] nums = {1, 2, 3, 4, 5};

for (int i = 0; i < nums.length; i++) {

System.out.println(nums[i]);

}

a. "1 2 3 4 5"

b. "5 4 3 2 1"

c. "1 3 5"

d. None of the above

Answer: a. "1 2 3 4 5"


What is the purpose of the "final" keyword in Java?

a. To prevent a class from being extended.

b. To prevent a method from being overridden.

c. To prevent a variable from being reassigned.

d. All of the above.

Answer: d. All of the above.


What is the output of the following code snippet?

String str1 = "Hello";

String str2 = "World";

String str3 = str1.concat(str2);

System.out.println(str3);

a. "HelloWorld"

b. "WorldHello"

c. "Hello"

d. None of the above

Answer: a. "HelloWorld"


What is inheritance in Java?

a. Inheritance is a way to define a new class based on an existing class.

b. Inheritance is a way to create a new instance of a class.

c. Inheritance is a way to restrict access to a method or variable.

d. None of the above.

Answer: a. Inheritance is a way to define a new class based on an existing class.


What is polymorphism in Java?

a. Polymorphism is the ability of an object to take on many forms.

b. Polymorphism is the ability of a method to be implemented in different ways by different classes.

c. Polymorphism is the ability of a variable to store different types of data.

d. None of the above.

Answer: b. Polymorphism is the ability of a method to be implemented in different ways by different classes.


What is the output of the following code snippet?

int x = 5;

int y = x++;

System.out.println(y);

a. 4

b. 5

c. 6

d. None of the above

Answer: b. 5


What is the purpose of the "super" keyword in Java?

a. To refer to the current object.

b. To create a new object.

c. To access a static variable.

d. To call a method or constructor in the superclass.

Answer: d. To call a method or constructor in the superclass.


What is the output of the following code snippet?

int[] nums = {1, 2, 3, 4, 5};

int sum = 0;

for (int i = 0; i < nums.length; i++) {

sum += nums[i];

}

System.out.println(sum);

a. 1

b. 5

c. 15

d. None of the above

Answer: c. 15


What is encapsulation in Java?

a. Encapsulation is the ability of an object to take on many forms.

b. Encapsulation is a way to define a new class based on an existing class.

c. Encapsulation is the hiding of implementation details from users of a class.

d. None of the above.

Answer: c. Encapsulation is the hiding of implementation details from users of a class.


What is the difference between a class and an object in Java?

a. A class is a blueprint for an object, while an object is an instance of a class.

b. A class is an instance of an object, while an object is a blueprint for a class.

c. A class and an object are the same thing.

d. None of the above.

Answer: a. A class is a blueprint for an object, while an object is an instance of a class.


What is a constructor in Java?

a. A constructor is a method that is called when an object is created to initialize its state.

b. A constructor is a method that is called when an object is destroyed to clean up its state.

c. A constructor is a method that is called when a class is loaded into memory.

d. None of the above.

Answer: a. A constructor is a method that is called when an object is created to initialize its state.


What is the output of the following code snippet?

int x = 5;

int y = ++x;

System.out.println(y);

a. 4

b. 5

c. 6

d. None of the above

Answer: c. 6


What is the purpose of the "this" keyword in Java?

a. To create a new object.

b. To access a static variable.

c. To call a method or constructor in the superclass.

d. To refer to the current object.

Answer: d. To refer to the current object.


What is a static variable in Java?

a. A variable that is shared among all instances of a class.

b. A variable that can only be accessed from within a method.

c. A variable that is created when an object is instantiated.

d. None of the above.

Answer: a. A variable that is shared among all instances of a class.


What is an interface in Java?

a. An interface is a way to define a new class based on an existing class.

b. An interface is a way to restrict access to a method or variable.

c. An interface is a collection of abstract methods that can be implemented by classes.

d. None of the above.

Answer: c. An interface is a collection of abstract methods that can be implemented by classes.


What is the difference between an abstract class and an interface in Java?

a. An abstract class can have method implementations, while an interface can only have method signatures.

b. An abstract class can be instantiated, while an interface cannot.

c. An abstract class can only extend one other class, while an interface can extend multiple interfaces.

d. None of the above.

Answer: a. An abstract class can have method implementations, while an interface can only have method signatures.


What is the output of the following code snippet?

int x = 10;

while (x > 0) {

System.out.println(x);

x--;

}

a. 1 2 3 4 5 6 7 8 9 10

b. 10 9 8 7 6 5 4 3 2 1

c. 0

d. None of the above

Answer: b. 10 9 8 7 6 5 4 3 2 1


What is the output of the following code snippet?

String s1 = "hello";

String s2 = "world";

System.out.println(s1 + " " + s2);

a. "helloworld"

b. "hello world"

c. "world hello"

d. None of the above

Answer: b. "hello world"


What is the difference between a StringBuilder and a StringBuffer in Java?

a. A StringBuilder is thread-safe, while a StringBuffer is not.

b. A StringBuffer is more efficient than a StringBuilder.

c. A StringBuilder is mutable, while a StringBuffer is immutable.

d. None of the above.

Answer: a. A StringBuilder is thread-safe, while a StringBuffer is not.


What is the purpose of the "final" keyword in Java?

a. To indicate that a variable or method cannot be modified or overridden.

b. To indicate that a class cannot be extended.

c. To indicate that a variable or method can only be accessed from within the same class.

d. None of the above.

Answer: a. To indicate that a variable or method cannot be modified or overridden.


What is a package in Java?

a. A way to group related classes and interfaces together.

b. A way to restrict access to a method or variable.

c. A way to define a new class based on an existing class.

d. None of the above.

Answer: a. A way to group related classes and interfaces together.


What is the output of the following code snippet?

int x = 5;

int y = 2;

double z = (double) x / y;

System.out.println(z);

a. 2

b. 2.5

c. 2.0

d. None of the above

Answer: b. 2.5


What is the purpose of the "super" keyword in Java?

a. To create a new object.

b. To access a static variable.

c. To call a method or constructor in the superclass.

d. To refer to the current object.

Answer: c. To call a method or constructor in the superclass.


What is an abstract method in Java?

a. A method that is declared with the "abstract" keyword and has no implementation.

b. A method that is declared with the "final" keyword and cannot be overridden.

c. A method that is declared with the "static" keyword and can be accessed without an object.

d. None of the above.

Answer: a. A method that is declared with the "abstract" keyword and has no implementation.


What is the difference between an instance variable and a class variable in Java?

a. An instance variable is shared among all instances of a class, while a class variable is not.

b. An instance variable is declared with the "static" keyword, while a class variable is not.

c. An instance variable is created when an object is instantiated, while a class variable is created when the class is loaded.

d. None of the above.

Answer: c. An instance variable is created when an object is instantiated, while a class variable is created when the class is loaded.


What is the purpose of the "break" keyword in Java?

a. To terminate a loop or switch statement.

b. To skip the current iteration of a loop.

c. To continue with the next iteration of a loop.

d. None of the above.

Answer: a. To terminate a loop or switch statement.


What is the output of the following code snippet?

int x = 5;

int y = 3;

if (x > y) {

System.out.println("x is greater than y");

} else if (x < y) {

System.out.println("x is less than y");

} else {

System.out.println("x is equal to y");

}

a. "x is greater than y"

b. "x is less than y"

c. "x is equal to y"

d. None of the above

Answer: a. "x is greater than y"


What is the purpose of the "this" keyword in Java?

a. To create a new object.

b. To access a static variable.

c. To refer to the current object.

d. To call a method or constructor in the superclass.

Answer: c. To refer to the current object.


What is the difference between a while loop and a do-while loop in Java?

a. A while loop executes the code block at least once, while a do-while loop may not execute the code block at all.

b. A while loop is more efficient than a do-while loop.

c. A while loop is used for iterating over arrays, while a do-while loop is used for iterating over collections.

d. None of the above.

Answer: a. A while loop executes the code block at least once, while a do-while loop may not execute the code block at all.


What is the purpose of the "instanceof" operator in Java?

a. To check if an object is an instance of a specific class or interface.

b. To convert a primitive type to its corresponding wrapper class.

c. To concatenate two strings.

d. None of the above.

Answer: a. To check if an object is an instance of a specific class or interface.


What is a constructor in Java?

a. A method that has no return type.

b. A method that is declared with the "static" keyword.

c. A special method that is called when an object is instantiated.

d. None of the above.

Answer: c. A special method that is called when an object is instantiated.


What is the output of the following code snippet?

int x = 10;

int y = 5;

if (x > y) {

if (x == 10) {

System.out.println("x is equal to 10");

} else {

System.out.println("x is greater than 5");

}

} else {

System.out.println("x is less than or equal to 5");

}

a. "x is equal to 10"

b. "x is greater than 5"

c. "x is less than or equal to 5"

d. None of the above

Answer: a. "x is equal to 10"


What is the difference between a superclass and a subclass in Java?

a. A superclass is a class that is derived from a subclass, while a subclass is a class that is derived from a superclass.

b. A superclass is a class that has more functionality than a subclass, while a subclass is a class that has less functionality than a superclass.

c. A superclass is a class that is higher up in the class hierarchy than a subclass, while a subclass is a class that is lower down in the class hierarchy than a superclass.

d. None of the above.

Answer: c. A superclass is a class that is higher up in the class hierarchy than a subclass, while a subclass is a class that is lower down in the class hierarchy than a superclass.


What is the purpose of the "super" keyword in Java?

a. To create a new object.

b. To access a static variable.

c. To refer to the current object.

d. To call a method or constructor in the superclass.

Answer: d. To call a method or constructor in the superclass.


What is the difference between an abstract class and an interface in Java?

a. An abstract class can have constructor implementations, while an interface cannot.

b. An abstract class can have instance variables, while an interface cannot.

c. An abstract class can have method implementations, while an interface cannot.

d. None of the above.

Answer: c. An abstract class can have method implementations, while an interface cannot.


What is the purpose of the "static" keyword in Java?

a. To create a new object.

b. To access an instance variable.

c. To declare a variable or method that belongs to the class, rather than to instances of the class.

d. None of the above.

Answer: c. To declare a variable or method that belongs to the class, rather than to instances of the class.


What is the output of the following code snippet?

int[] arr = {1, 2, 3};

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

a. "1 2 3"

b. "1, 2, 3"

c. "123"

d. None of the above

Answer: a. "1 2 3"


What is the purpose of the "final" keyword in Java?

a. To indicate that a method cannot be overridden by a subclass.

b. To indicate that a class cannot be subclassed.

c. To indicate that a variable cannot be reassigned once it has been initialized.

d. All of the above.

Answer: d. All of the above.


What is the difference between "==" and ".equals()" in Java?

a. "==" compares the memory addresses of two objects, while ".equals()" compares the values of the objects.

b. "==" compares the values of two objects, while ".equals()" compares the memory addresses of the objects.

c. "==" is used to compare primitive data types, while ".equals()" is used to compare objects.

d. None of the above.

Answer: a. "==" compares the memory addresses of two objects, while ".equals()" compares the values of the objects.


What is the output of the following code snippet?

String str1 = "hello";

String str2 = "hello";

System.out.println(str1 == str2);

a. true

b. false

c. Error

d. None of the above

Answer: a. true


What is the output of the following code snippet?

String str1 = "hello";

String str2 = new String("hello");

System.out.println(str1 == str2);

a. true

b. false

c. Error

d. None of the above

Answer: b. false


What is the output of the following code snippet?

int num = 5;

System.out.println(num++);

System.out.println(num);

a. "5 6"

b. "6 5"

c. "5 5"

d. None of the above

Answer: a. "5 6"


What is the output of the following code snippet?

int num = 5;

System.out.println(++num);

System.out.println(num);

a. "5 6"

b. "6 5"

c. "6 6"

d. None of the above

Answer: c. "6 6"


Which of the following is a valid declaration of a two-dimensional array in Java?

a. int[][] arr = new int[3][3];

b. int[][] arr = { {1, 2}, {3, 4}, {5, 6} };

c. int[] arr = {1, 2, 3};

d. All of the above.

Answer: a. int[][] arr = new int[3][3];


What is the output of the following code snippet?

int[][] arr = { {1, 2}, {3, 4}, {5, 6} };

System.out.println(arr[1][0]);

a. 1

b. 2

c. 3

d. 4

Answer: c. 3


What is the output of the following code snippet?

for (int i = 0; i < 5; i++) {

if (i == 3) {

continue;

}

System.out.println(i);

}

a. "0 1 2 4"

b. "0 1 2 3 4"

c. "0 1 2"

d. "0 1 2 3"

Answer: a. "0 1 2 4"


What is the output of the following code snippet?

for (int i = 0; i < 5; i++) {

if (i == 3) {

break;

}

System.out.println(i);

}

a. "0 1 2 4"

b. "0 1 2 3 4"

c. "0 1 2"

d. "0 1 2 3"

Answer: c. "0 1 2"


What is the purpose of the "static" keyword in Java?

a. To indicate that a method or variable belongs to a class rather than an instance of the class.

b. To indicate that a method or variable can only be accessed within the same package.

c. To indicate that a method or variable cannot be overridden by a subclass.

d. None of the above.

Answer: a. To indicate that a method or variable belongs to a class rather than an instance of the class.


What is a constructor in Java?

a. A method that is used to destroy an object.

b. A method that is used to create an object.

c. A variable that is used to store the state of an object.

d. A variable that is used to store the behavior of an object.

Answer: b. A method that is used to create an object.


Which of the following is not a primitive data type in Java?

a. int

b. boolean

c. float

d. String

Answer: d. String


Which of the following is a valid declaration of a String in Java?

a. String str = "hello";

b. String str = new String("hello");

c. Both a and b.

d. None of the above.

Answer: c. Both a and b.


What is the difference between a StringBuilder and a StringBuffer in Java?

a. A StringBuilder is mutable, while a StringBuffer is immutable.

b. A StringBuilder is not thread-safe, while a StringBuffer is thread-safe.

c. A StringBuilder has a larger capacity than a StringBuffer.

d. None of the above.

Answer: b. A StringBuilder is not thread-safe, while a StringBuffer is thread-safe.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Top Post Ad

Below Post Ad