img
Java - I

Java is a versatile programming language used for developing a wide range of applications. It offers strong support for object-oriented programming and is known for its robustness and scalability. Java is widely employed by companies across industries.

Last updated on:- 7th June 2023


1.
What is Java ?
A programming language
A database management system
An operating system
A web browser

Java is a popular object-oriented programming language used to develop a wide range of applications, including desktop, web, and mobile applications.


2.
What is the purpose of the 'public' keyword in Java ?
To declare a class
To declare a variable
To declare a method accessible to other classes
To declare a constant

The 'public' keyword in Java is an access modifier that allows a method to be accessed by other classes. It provides the highest level of visibility and allows the method to be called from any other class.


3.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int x = 5; System.out.println(x++); System.out.println(x); } }
5 6
5 5
6 5
6 6

The 'x++' statement is a post-increment operation, which means that the value of 'x' is first printed (5) and then incremented to 6. Therefore, the output is: 5 6


4.
What is the correct way to declare and initialize an array in Java ?
int[] numbers = {1, 2, 3};
int[] numbers = new int[3] {1, 2, 3};
int numbers[] = {1, 2, 3};
int numbers[] = new int[] {1, 2, 3};

Option A is the correct way to declare and initialize an array in Java. It uses the curly braces to specify the initial values of the array elements without explicitly specifying the size of the array.


5.
Which of the following is a valid Java identifier ?
_myVar
123abc
my-var
class

Option A, '_myVar', is a valid Java identifier. It starts with an underscore and can be followed by letters, digits, or underscores. Options B, C, and D are invalid identifiers due to the use of digits at the beginning, the hyphen character, and a reserved keyword, respectively.


6.
What is the purpose of the 'static' keyword in Java ?
To declare a variable
To declare a method
To declare a class
To declare a constant

The 'static' keyword in Java can be used to declare a static variable. Static variables are associated with the class rather than instances of the class. They are shared among all instances of the class and can be accessed without creating an object of the class.


7.
Which of the following is true about the Java Virtual Machine (JVM) ?
It is a physical machine that runs Java programs
It is a software program that executes Java bytecode
It is a programming language
It is an integrated development environment (IDE)

The JVM (Java Virtual Machine) is a software program that executes Java bytecode. It provides a runtime environment for Java applications, allowing them to be platform-independent and run on any device or operating system that has a compatible JVM.


8.
Which keyword is used to create an instance of a class in Java ?
new
instance
create
instantiate

The 'new' keyword is used to create an instance of a class in Java. It dynamically allocates memory for the object and initializes its state using the class's constructor.


9.
Which of the following is a primitive data type in Java ?
String
Integer
Float
Boolean

Option D, 'Boolean', is a primitive data type in Java. Primitive data types are the most basic data types provided by the language. They include boolean, byte, short, int, long, float, double, and char.


10.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { String text = "Hello, World !"; System.out.println(text.length()); } }
12
13
14
15

The 'length()' method in Java returns the number of characters in a string. In the given code, the string 'Hello, World !' has 14 characters, so the output is: 14


11.
Which of the following is used to create an object with a copy of the values of another object in Java ?
new
clone
copy
copyOf

The 'clone' method in Java is used to create an object with a copy of the values of another object. It performs a shallow copy of the object, meaning that the new object and the original object will share the same references to other objects unless a deep copy is implemented explicitly.


12.
What is the purpose of the 'break' statement in Java ?
To exit a loop or switch statement
To skip the current iteration of a loop
To jump to a specific label in a loop or switch statement
To terminate the program

The 'break' statement in Java is used to exit a loop or switch statement. When encountered, the 'break' statement terminates the execution of the innermost loop or switch statement and continues with the next statement after the loop or switch block.


13.
Which of the following is a valid way to declare a constant in Java ?
final int MAX_VALUE = 100;
int MAX_VALUE = 100;
const int MAX_VALUE = 100;
static int MAX_VALUE = 100;

Option A is a valid way to declare a constant in Java. The 'final' keyword is used to indicate that the value of the variable cannot be changed once it is assigned. By convention, constant names are written in uppercase.


14.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int x = 10; if (x > 5) { System.out.println("Hello"); } System.out.println("World"); } }
Hello World
World Hello
Hello
World

The 'if' statement in Java is used to conditionally execute a block of code. In the given code, the condition 'x > 5' is true (as x is 10), so the code within the 'if' block is executed, resulting in the output: Hello World


15.
What is the purpose of the 'equals()' method in Java ?
To compare two primitive data types
To compare two objects for equality
To concatenate strings
To convert a string to lowercase

The 'equals()' method in Java is used to compare two objects for equality. It is commonly overridden in classes to provide custom comparison logic. By default, the 'equals()' method compares the memory references of objects, but it can be overridden to compare the contents of objects.


16.
Which of the following is used to read input from the user in Java ?
System.in
System.out
System.err
Scanner

The 'Scanner' class in Java is used to read input from the user. It provides various methods to read different types of input, such as 'nextInt()' for reading an integer, 'nextLine()' for reading a line of text, and so on.


17.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int[] numbers = {1, 2, 3}; System.out.println(numbers[3]); } }
Error
ArrayIndexNotFound
ArrayIndexOutOfBoundsException
3

In Java, arrays are zero-indexed, meaning that the first element has an index of 0. In the given code, 'numbers[3]' tries to access the fourth element of the 'numbers' array, which does not exist. This results in an 'ArrayIndexOutOfBoundsException' at runtime.


18.
Which of the following is true about constructors in Java ?
They have a return type
They are used to initialize objects
They can be inherited
They can be static

Constructors in Java are special methods that are used to initialize objects of a class. They have the same name as the class and do not have a return type. Constructors are automatically called when an object is created using the 'new' keyword.


19.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int a = 5; int b = 7; int result = (a > b) ? a : b; System.out.println(result); } }
5
7
12
Compilation error

The ternary operator in Java (?:) is a shorthand way of writing an 'if-else' statement. In the given code, the condition 'a > b' evaluates to false, so the value of 'b' is assigned to the 'result' variable. Therefore, the output is: 7


20.
Which of the following is used to handle exceptions in Java ?
try-catch
if-else
for loop
switch statement

The 'try-catch' statement in Java is used to handle exceptions. It allows you to catch and handle specific exceptions that may occur during the execution of a block of code. The 'try' block contains the code that may throw an exception, and the 'catch' block catches and handles the exception.


21.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int x = 0; while (x < 5) { System.out.print(x + " "); x++; } } }
0 1 2 3 4
1 2 3 4 5
0 1 2 3 4 5
1 2 3 4

The given code uses a 'while' loop to iterate as long as the condition 'x < 5' is true. The variable 'x' starts from 0 and increments by 1 in each iteration. Therefore, the loop will execute five times, printing the values of 'x' from 0 to 4. The output is: 0 1 2 3 4


22.
Which of the following is used to convert a primitive data type to a string in Java ?
toString()
valueOf()
parse()
convert()

The 'valueOf()' method in Java is used to convert a primitive data type to a string. It is available for all primitive data types and can be called directly on the data type, such as 'Integer.valueOf()', 'Double.valueOf()', and so on.


23.
What is the purpose of the 'finally' block in Java exception handling ?
To handle exceptions
To specify the code to be executed if an exception occurs
To clean up resources
To control the flow of execution

The 'finally' block in Java exception handling is used to specify code that should be executed regardless of whether an exception occurs or not. It is commonly used to release resources, such as closing a file or releasing a database connection, to ensure proper cleanup.


24.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int[] numbers = {1, 2, 3}; for (int num : numbers) { System.out.print(num + " "); } } }
1 2 3
1, 2, 3
1 2 3
Compilation error

The given code uses a 'for-each' loop to iterate over the elements of the 'numbers' array. In each iteration, the current element is assigned to the 'num' variable, and it is printed followed by a space. Therefore, the output is: 1 2 3


25.
Which of the following is used to convert a string to an integer in Java ?
toString()
valueOf()
parse()
convert()

The 'parse()' method in Java is used to convert a string to an integer. It is available for numeric data types, such as 'Integer.parseInt()', 'Double.parseDouble()', and so on. It parses the string representation of a number and returns the corresponding numeric value.


26.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int x = 5; int y = x++; System.out.println(x + " " + y); } }
5 5
5 6
6 5
6 6

The '++' operator in Java is used to increment a variable by 1. In the given code, 'x++' is a post-increment operation, which means that the value of 'x' is first assigned to 'y' and then incremented. Therefore, 'y' will have the original value of 'x' (5), and 'x' will be incremented to 6. The output is: 6 5


27.
Which of the following is used to perform a bitwise AND operation in Java ?
&
&&
|
||

The '&' operator in Java is used to perform a bitwise AND operation. It compares the corresponding bits of two operands and produces a result in which each bit is set to 1 if both corresponding bits are 1; otherwise, the bit is set to 0.


28.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { int x = 10; int y = 20; boolean result = (x != y); System.out.println(result); } }
true
false
10
Compilation error

The '!=' operator in Java is used to check if two values are not equal. In the given code, the values of 'x' and 'y' are different (10 and 20), so the condition 'x != y' is true. Therefore, the value of the 'result' variable is true, and it is printed as the output.


29.
Which of the following is true about the 'this' keyword in Java ?
It refers to the current object
It refers to the superclass object
It refers to the subclass object
It refers to a static variable

The 'this' keyword in Java is used to refer to the current object. It can be used to access instance variables and invoke methods of the current object. It is often used to disambiguate between instance variables and parameters with the same name.


30.
What is the output of the following Java code ? public class Main { public static void main(String[] args) { String str = "Hello, World!"; System.out.println(str.length()); } }
12
13
14
Compilation error

The 'length()' method in Java is used to get the length (number of characters) of a string. In the given code, the string 'str' contains 13 characters, including spaces and punctuation. Therefore, the output is: 13

img

"Embrace curiosity, unlock knowledge"

Copyright © 2023 - Some rights reserved

Made with

in INDIA