Java Questions
Java Questions
int a = 90;
switch (a) {
case 50 + 40:
System.out.println("Case 50+40");
break;
case 80 + 10:
System.out.println("Case 80+10");
break;
default:
System.out.println("Default case");
}
}
}
answer CTE
Options:
a) only if the method's return type is other than void.
b) it will result in a Compilation Time Error (CTE).
c) only if the method is marked as static.
d) We can use method call in case labels in all scenarios without any issues.
What does the equality operator (==) do when used to compare two String objects in
Java?
Options:
a) It checks whether the contents of the two String objects are equal.
b) It checks whether the two String objects refer to the same memory location.
c) We cannot use equality opearator to compare to String Objects
d) It automatically calls the equals() method to compare the two String objects.
Options:
a) System.out.print() will cause a Compilation Time Error (CTE), but
System.out.println() will work fine.
b) Both System.out.print() and System.out.println() will cause a Compilation Time
Error (CTE).
c) System.out.print() will work fine, but System.out.println() will cause a
Compilation Time Error (CTE).
d) Both System.out.print() and System.out.println() can be used without arguments;
System.out.println() prints a blank line, and System.out.print() prints nothing.
char
int
compile time error
Nothing
default
b
a
compile time error
4)
import java.util.Scanner;
public class Test4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter String1 : ");
String str1 = sc.next();
System.out.println("Enter String2 : ");sc.next();
String str2 = sc.nextLine();
System.out.println(str1+str2);
}
}
if(test1(test2('a')))
{
System.out.println("Kadavuley");
}
else
System.out.println("Ajithey");
}
public static boolean test1(String str)
{
return true;
}
public static String test2(int a)
{
return a+"Hello";
}
}
Ajithey
Kadavuley
Compile Time Error
No output
overLoad((char)97+1);
}
public static void overLoad(int a)
{
System.out.println(a);
}
public static void overLoad(char ch)
{
System.out.println(ch);
}
}
b
Compile Time Error
98
97
if(true)
{
int a =10;
System.out.print(a);
}
int a = 20;
System.out.print(a);
}
}
10
20
Compile Time Error
1020
8) Which are all the following we cannot use in the method body as a statement
Condition
Switch statement
Expression
Import statement
9) What is the output for the following program
public class Test1 {
if(true)
System.out.println("if block");
else if(true)
System.out.println("else if block");
else
System.out.println("else block");
}
}
else if block
else block
if block
Compile Time Error
boolean b = (boolean)true;
if(b)
System.out.println("if block");
else
System.out.println("else block");
}
}
else block
Compile Time Error
if block
Run Time Error
1021
1122
1121
1020
switch((int)10L)
{
case (int)10.15:
System.out.print(20);
case (char)11.15:
System.out.print(40);
return;
default:
System.out.print(60);
}
}
}
System.out.println(Math.max(demo1(), demo2()));
}
public static int demo1()
{
return 10;
}
public static char demo2()
{
return 'a';
}
}
a
Compile Time Error
10
97
System.out.print(demo(true));
}
public static int demo(boolean b)
{
if(b)
return 10;
else
return 20;
System.out.print("TheEnd");
}
}
10TheEnd
Compile Time Error
TheEnd
20TheEnd
System.out.println((char)(int)'a');
}
}
97
a
Compile Time Error
None of the Above
19) Which are the following is compile time error (with respect to method
declaration)
20) What is the res value from the following snippet code
int a = 45;
int b = 20;
int res = a+=a=b;
20
90
65
40
23) What should be the operands for compound assignment operators( ex:- Syntax :
op1 += op2)
expression - 10+10.15+'a'
Type Promotion
It will do narrowing
It will throw compile time error
It will execute the expression
25) What will happen when we perform addition operation between char data and
String data
26) What is the value that the following printing statement will print
System.out.println((10>20?false:true) ? true:false);
false
true
Compile Time Error
None of the above
27) Which are all the following are correct regarding to NOT operator
0
10.0
0.0
10
30) In which datatype we can store all the number type of data
String
int
float
double
class Demo {
Given the following Java code, what will be the output when executed?
class Main {
public static void main(String[] args) {
int main = 0;
System.out.println(main);
}
}Options:
A) main
B) 0
C) Compilation error: Cannot use main as a variable name
D) Runtime error
Correct Answer:
B) 0
Five
Ten
Five
Five
Ten
Default
7.What will happen if no case matches in a switch block without a default case?
Options:
The program will throw an exception.
The first case will be executed by default.
Nothing will happen; the control exits the switch block.
The program will terminate.
Ans : Nothing will happen; the control exits the switch block.
8.What happens if multiple cases in a switch block have the same constant value?
10
11
compile time error
Thankyou
even
Greater than 10
Divisible by 3
compile time error
Hii
Bye
ans Bye
int a = 3, b = 6;
if (a + b > 10 && b - a < 5) {
if ((a * b) % 2 == 0)
System.out.println("Condition 1");
else
System.out.println("Condition 2");
} else if (b % a == 0 || b + a < 15) {
if (b / a == 2)
System.out.println("Condition 3");
else
System.out.println("Condition 4");
} else {
System.out.println("Condition 5");
}
Condition 1
Condition 2
Condition 3
Condition 4
ans : Condition 3
Hii
10
compile time error
None of the above
10
Bye
compile time error
None of the above
15.Can you overload a method based on the return type alone in Java?
Options:
Yes, it is allowed.
No, method overloading requires a difference in parameter type or number, not
return type.
Yes, but only if the return type is void.
No, return type is never considered for method overloading.
ans : No, method overloading requires a difference in parameter type or number, not
return type.
ans : It specifies that the method does not return any value.
default : System.out.println("Invalid);
ten
eleven
Invalid
compile time error
19.
int a =20;
switch(a){
default : System.out.println("Invalid);
eleven
ten
Invalid
compile time error
ans : ten
20.
Math class is present in which package in Java?
java.math
java.util
java.lang
java.io
ans : java.lang
ans : It is used to read input from various sources like keyboard, file, and
string.
22.
Which method helps to extract a character from the end user in Java?
Options:
nextLine()
nextChar()
next()
next().charAt(0)
Answer:
next().charAt(0)
23.
Answer:
A. next() reads a single word and stops at a whitespace, while nextLine() reads an
entire line including spaces.
24.int a = 10
;System.out.print(a)
;int b = 20;
System.out.print(b);
10 20
None
compile time error
20
ans 10 20
int a = 10;
if(!true)
a = a+5;
a = a-5;
System.out.println(a);
10
5
15
compile time error
ans 5
26.
What is the significance of datatype in Java?
It defines the size and type of data that can be stored in a variable.
It determines the visibility of a class.
It controls the flow of the program.
It allows for dynamic memory allocation during runtime.
27.
Is it mandatory to use type casting during narrowing in Java?
Answer:
A. Yes, it is mandatory to use explicit type casting to avoid data loss.
28.
What is the result of the expression true && !(true) in Java?
Options:
true
false
true && true
false && false
ans false
29.
int x = 9, y = 12;
int a = 2, b = 4, c = 6;
System.out.println(exp);
278
277
178
123
ans 278
30
Here’s the MCQ based on the question "Why do we need operators in Java?"
Answer: a) Example.display();
Q11: What happens if you try to call a non-static method from a static method
without creating an object?
a) It will compile successfully.
b) It will result in a compile-time error.
c) It will result in a runtime error.
d) It will work if the non-static method is also declared static.
A. Compilation error.
B. life
C. universe
D. everything
Answer:D
Q15: Which of the following is a valid return type for a method in Java?
a) void
b) int
c) String
d) All of the above
Answer: b) nullWorld
Q19: What is the correct way to create an object of a class named Car?
a) Car myCar = new Car();
b) Car myCar;
c) new Car myCar;
d) Car myCar = Car();
Answer: c) A member that can be accessed without creating an instance of the class.
Answer: c) -128
Answer: c) 312
Answer: b) 6.14
Answer: a) 6.0
Q26: Analyze the following code snippet. What will it print
public class Demo {
public static void main(String[] args) {
int x = 5;
x += ++x; // Pre-increment
System.out.println(x);
}
}
a) 10
b) 11
c) 12
d) Compilation error
Answer: b) 11
Q27: Which package must be imported to use the Scanner class in Java?
a) java.util
b) java.io
c) java.lang
d) java.scan
Answer: a) java.util
Q28: Which of the following is true regarding the close() method of the Scanner
class?
Q29: Which of the following is the correct syntax for importing a specific class
from a package?
a) import package.className;
b) import package.className.*;
c) import className from package;
d) import package.className;
A) Test Begin
65
Test End
Demo Begin
a:65
Demo End
131
B) Test Begin
A
Test End
Demo Begin
a:65
Demo End
131
C) Test Begin
A
Test End
Demo Begin
a:66
Demo End
132
B) Calculating...
11
C) Calculating...
12
D) Error
Answer: C) Calculating...
12
A)
Compute Start
value: 10
Modify Start
num: 10
Modify End
10
B)
Compute Start
value: 10
Modify Start
num: 10
Modify End
17
C)
Compute Start
value: 10
Modify Start
num: 10
Modify End
7
D)
Compute Start
value: 10
Modify Start
num: 10
Modify End
20
correct answer :C
Q35. What will the following code output? Explain the sequence of method calls.
public class Prog4 {
public static void main(String[] args) {
System.out.println(check('B'));
}
A)
Check Start
Character: B
Check End
Perform Start
Character: B
Perform End
66
B)
Check Start
Character: B
Check End
Perform Start
Character: B
Perform End
65
C)
Check Start
Character: B
Check End
Perform Start
Character: B
Perform End
68
D)
Check Start
Character: B
Check End
Perform Start
Character: B
Perform End
70
Correct Answer: B
a) return 10>20 ;
b) return 'a'+10;
c) return (int a = 10);
d) return 10>20 ? 10 : 20;
a) 20
b) compile time error
c) 10
d) None of the Above
a) Interpreter
b) JIT
c) Java Compiler
d) JVM
a) import java.lang.Scanner;
b) Import java.util.Scanner;
c) import java.util.*;
d) import java.util.Scanner;
9) Which members can be called from one class into another class?
10) which one is the correct when the return type of the method is void
a) a
b) 97
c) Compile time error
d) None of the Above
a) Value
b) Variable
c) Statement
d) Expression
a) Static variables
b) Local variables
c) non static variables
d) None of the Above
a) No output
b) Compile time error
c) 30
d) None of the Above
a) byte>short>int>long>float>double
b) byte<short<int<float<long<double
c) byte<short<int<long<float<double
d) byte<short<int<float<double<long
a) No output
b) Complie time error
c) It will not execute
d) All the best
a) JVM
b) Java compiler
c) Programmer
d) JIT compiler
a) Type casting
b) Self Updation
c) Reduce code redundancy
d) All of the Above
a) 13
b) 8
c) Complie time error
d) None of the Above
a) 22
b) 21
c) Complie time error
d) 20
24) What should be the operand for Increment and Decrement operator
26) What we cannot use in the place of Operand2 and Operand3 in Conditional
Operator
a) Expression
b) Statement
c) data
d) Variable
28)
int a = 10;
int b = 20;
int res = a+b;
What is the correct expression we have to use to print the output like 10+20=30
a) a+b=res
b) a+"+"+b+"="+res
c) a"+"b"="res
d) We cannot print like 10+20=30
a) &&
b) ++
c) +
d) %
a) Dot(.) Operator
b) Compound Assignment operators
c) Relational operators
d) Arithmetic Operators
a) keyword
b) identifier
c) Literal
d) Symbol
a) Predefined Word
b) Reserved word
c) Compliler aware word
d) All of the Above
These are the questions that I am going to add Do check people before adding yours
a) equals()
b) compareTo()
c) ==
d) compare()
Answer: a) equals()
Reason: The equals() method is used to compare the content of two strings.
a) 5
b) 6
c) 7
d) Compile-time error
Answer: b) 6
Reason: The ++x increments the value of x before it is assigned to y, so y becomes
6.
a) public
b) private
c) protected
d) package
Answer: d) package
Reason: There is no package access modifier; the default access modifier is called
package-private.
a) public
b) private
c) protected
d) package-private
Answer: d) package-private
Reason: If no access modifier is specified, the default is package-private, meaning
accessible within the same package.
6. What is the result of the expression true && false?
a) true
b) false
c) undefined
d) null
8.Which of the following statements correctly calls the method display(int number)?
A) display(number=5);
B) display(5);
C) int display(5);
D) display()=5;
A) 5 5
B) 5 4
C) 2 2
D) 5 1
Answer: B) 5 4
13.Guess the output for the following code snippet.
int x = 9, y = 12;
int a = 2, b = 4, c = 6;
int exp = (3 + 4 * x)/5 - 10 * (y - 5) * (a + b + c)/x + 9 * (4/x + (9 + x)/y);
System.out.println(exp);
A) -77
B) -67
C) -65
D) -10
Answer : A) -77
int x = 10 * (2 + (1 + 2 / 5));
int y = x * 2;
System.out.print(x + y < 10 ? "Hello" : "Java");
A) Hello
B) Java
C) Compilation Error
D) Runtime Error
Answer : B) Java
A) 21 5
B) 15 10
C) 30 5
D) 20 5
Answer : A)21 5
A) int 1x = 10;
B) int x1 = 10;
C) int x 10;
D) int = 10 x;
Answer: B) int x1 = 10;
float f = 5f;
System.out.println(f);
A) 5
B) 5.00
C) 5.0000
D) 5.0
Answer D) 5.0
int x = 5;
double y = 2.0;
System.out.println(x + y);
A) 7.0
B) 7
C) Compilation Error
D) 5.0
Answer: A) 7.0
A)70
B)80
C)90
D)100
Answer : C)90
23.Which of the following is not a valid use of the arithmetic operators in Java?
A) 'A' + 'B'
B) "Hello" + 10
C) true + 1
D) 10 / 2
Answer: C) true + 1
Explanation: Arithmetic operators cannot be used with boolean values. true + 1 will
result in a compile-time error.
24.What will be the output for the following code snippet?
String s = "Hello"
System.out.println(s+'a');
A) "Helloa"
B) "Hello97"
C) Compile time error
D) None of the above
Answer A) Helloa
A) 15Java510
B) 15Java15
C) 15Java510
D) Compile-time error
Answer A) 15Java510
class Test {
private static void display() {
System.out.println("Hello");
}
Answer : B) 8.0
30.
A) +
B) -
C) *
D) /
Answer D) /
31.
Which of the following is an example of a unary operator in Java?
A) >
B) ==
C) !
D) + and -
Answer: C) !
Explanation: The ! operator is a unary operator that negates the boolean value of
its operand.
32.
Which of the following statements about binary operators is true?
33.What is an Expression?
A) A statement that performs an action or calculation
B) A block of code that runs only under certain conditions
C) A sequence of operators and operands that evaluates to a value
D) A part of the program that defines the structure of the code
35.What is ASCII?
void myMethod(int x) {
// ...
}
b)
Java
int myMethod() {
// ...
}
int myMethod() {
// ...
}
c)
Java
void myMethod(int x) {
// ...
}
void yourMethod(int x) {
// ...
}
d)
Java
void myMethod(int x) {
// ...
}