0% found this document useful (0 votes)
13 views42 pages

Java Questions

The document contains a series of Java programming questions and code snippets, focusing on concepts like switch statements, method calls, and conditional statements. Each question is followed by multiple-choice options, with correct answers provided for some. The content is aimed at assessing knowledge of Java syntax and behavior, particularly in relation to control flow and data types.

Uploaded by

bhavanipriy73
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
0% found this document useful (0 votes)
13 views42 pages

Java Questions

The document contains a series of Java programming questions and code snippets, focusing on concepts like switch statements, method calls, and conditional statements. Each question is followed by multiple-choice options, with correct answers provided for some. The content is aimed at assessing knowledge of Java syntax and behavior, particularly in relation to control flow and data types.

Uploaded by

bhavanipriy73
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 42

public static void main(String[] args) {

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

Can you use a method call in a case label in Java?

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.

public class Demo {


public static void main(String[] args) {
System.out.println("" == null);
}
}
Options:
a) true
b) false
c) Compilation Error
d) RunTimeProblem

Can you use System.out.print() and System.out.println() without passing any


arguments in Java?

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.

1) What is the output for the following program


class Test1
{
public static void main(String[] args)
{
int a = 97;
switch(a)
{
case 97:
System.out.println("int");
break;
case 'a':
System.out.println("char");
break;
default:
System.out.println("Nothing");
}
}
}

char
int
compile time error
Nothing

2)What is the output for the following program


class Test2
{
public static void main(String[] args)
{
String str = false+"";
switch(str)
{
case ""+false:
System.out.println("true");
break;
case true+"":
System.out.println("false");
break;
default:
System.out.println("true and false");
}
}
}

true and false


false
true
compile time error

3)What is the output for the following program


class Test3
{
public static void main(String[] args)
{
int num = 97;
switch(num)
{
case demo('a'):
System.out.println('a');
break;
case demo('b'):
System.out.println('b');
break;
default:
System.out.println("dafault");
}
}
public static int demo(char ch)
{
return ch;
}
}

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);
}
}

What is the output of this program if user enters like this,


Enter String1 :
123
Enter String2 :
456

Compile Time Error


123456
123
456

5) What is the output for the following program


public class Test5 {

public static void main(String[] args) {

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

6) What is the output for the following program


public class Test6 {

public static void main(String[] args) {

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

7) What is the output for the following program


public class Test7 {

public static void main(String[] args) {

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 {

public static void main(String[] args) {

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

10) What is the output for the following program


public class Test1 {

public static void main(String[] args) {

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

11) What is if statement

Decision Making Statement


Control Transfer Statement
Looping Statement
Branching Statement

12) Decision Making Statement are used to

Execute the instructions


Skip the instruction
Execute or Skip the instructions
Execute the instruction based on condition

13) What is the output for the following program


public class Test1 {

public static void main(String[] args) {

int a = 10, b = 20;


int res = a>b++ && ++a<b ? b++ : b--;
System.out.print(a);
System.out.print(b);
}
}

1021
1122
1121
1020

14) What is the output for the following program


public class Test1 {

public static void main(String[] args) {

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);
}
}
}

Compile Time Error


20
204060
2040

15) Where we can use return statement

In the method body


In the case blocks of switch statement
In the class block (only inside class block)
outside the class block

16) What is the output for the following program


public class Test1 {

public static void main(String[] args) {

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

17) What is the output for the following program


public class Test1 {

public static void main(String[] args) {

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

18) What is the output for the following program


public class Test1 {

public static void main(String[] args) {

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)

public static void test(int a,b,c)


public static int demo(int b=20)
static public String class(int a)
void check(int a)

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

21) Which one is not the characteristic of a method


We can call a method multiple times
Method is a member of the class
To execute a method we need to invoke
For method we can create an instance

22) What the modifiers will do

Behavioural changes for java members


control the accessibility
It will help a method to return a value
It is used to call a method

23) What should be the operands for compound assignment operators( ex:- Syntax :
op1 += op2)

op1-variable and op2-variable


op1-value and op2-variable
op1-variable and op2-expression
op1-variable and op2-value

24) While compiling the following expression what a compiler will do

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

String data will be concatenated with ascii value of char data


String data will be concatenated with char data
Compile Time Error
None of the above

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

It will work with only Boolean data


It is used to negate the Boolean data
We can use NOT operator with condition
It will return Boolean

28) How switch statement will work

It will use == operator to compare switch and case block values


break statement is optional
We can use Boolean data in switch
For case block we can pass variable
29) What is the value that the following printing statement will print
System.out.println(10%10.5);

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 {

public static void temp() {


int temp = 90;
System.out.println(temp+=10);
}

public static void main(String[] args) {


temp();
}
}
Options:
A) 90
B) 100
C) Compilation error: Variable temp cannot be used in the method.
D) Compilation error :Because we cannot use compound assignment operator in
printing statement

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

Questions of Hima vamsi for monday assessment

1.Guess the output for the following code snippet


int num = 5;
switch (num) {
case 1:
System.out.println("One");
case 2:
System.out.println("Two");
case 5:
System.out.println("Five");
case 10:
System.out.println("Ten");
break;
default:
System.out.println("Default");
break;
}

Five
Ten

Five

Five
Ten
Default

compile time error

2.Guess the output for the following code snippet

int age = 25;


switch (age) {
case age < 18:
System.out.println("Underage");
break;
case age >= 18 && age < 60:
System.out.println("Adult");
break;
default:
System.out.println("Senior");
}

Logical error in switch cases


Compilation error due to boolean expressions in case labels
The default case will never execute
No error

3.Guess the output for the following code snippet

int a = 5, b = 10, c = 15;


if (a > b || (b++ > c)) {
System.out.println("Condition 1: True");
} else if (b > c && (++a < c)) {
System.out.println("Condition 2: True");
} else {
System.out.println("Condition 3: True");
}
System.out.println("a: " + a + ", b: " + b + ", c: " + c);

Condition 1: True followed by a: 5, b: 11, c: 15


Condition 2: True followed by a: 6, b: 11, c: 15
Condition 3: True followed by a: 6, b: 11, c: 15
Condition 3: True followed by a: 5, b: 11, c: 15

4.Which of the following is not allowed in a switch statement in Java?


Options:
A. char as a case value
B. boolean as a case value
C. String as a case value
D. byte as a case value

ans : boolean as a case value

5. How is the default case executed in a switch statement?


Options:
A. It is always executed first.
B. It is executed only when no matching case is found.
C. It is mandatory to include the default case in a switch block.
D. It is executed before the first matching case.

ans : It is executed only when no matching case is found.

6.Which of the following is true about the switch statement in Java?


Options:
A case label can be a constant or a variable.
The default label must always be present.
Fall-through behavior occurs when break is missing.
Only primitive data types are allowed in the switch statement.

Ans : Fall-through behavior occurs when break is missing.

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?

The compiler will ignore duplicate cases.


The program will throw a runtime exception.
The compiler will generate an error.
The default case will execute automatically.

9.Guess the output for the following code snippet


int a = 10;
if(a>5)
System.out.println(a);
System.out.println(a+1);
else
System.out.println("Thankyou");

10
11
compile time error
Thankyou

ans : compile time error

10.Guess the output for the following code snippet


int num = 9;
else if (num % 2 == 0)
System.out.println("Even");
else if (num > 10)
System.out.println("Greater than 10");
else if (num % 3 == 0)
System.out.println("Divisible by 3");
else
System.out.println("None");

even
Greater than 10
Divisible by 3
compile time error

ans : compile time error

11.Guess the output for the following code snippet


if(!true)
System.out.println("Hii");
System.out.println("Bye");
Hii Bye

Hii

Bye

compile time error

ans Bye

12.Guess the output for the following code snippet

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

13. Guess the output for the following code snippet


if(10>2){
int a = 10;
System.out.println("Hii");
}else{
System.out.println(a);
}

Hii
10
compile time error
None of the above

ans : compile time error

14.Guess the output for the following code snippet


if(!true){
int a = 10;
System.out.println(a);
break;
}else{
System.out.println("Bye");
}

10
Bye
compile time error
None of the above

ans : compile time error

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.

16.What does void specify in a method

It indicates that the method can return multiple values.


It specifies that the method does not return any value.
It is used to declare a method that takes no parameters.
It specifies the type of return value for the method.

ans : It specifies that the method does not return any value.

17.Why do we need decision statements in Java?


To allow the program to perform different actions based on different conditions.
To increase the speed of the program execution.
To define the return type of a method.
To define the parameters of a method.

ans : To allow the program to perform different actions based on different


conditions.

18.Guess the output for the following code snippet


int a = 10;
switch(a){

case (byte)10.2 : System.out.println("ten");


break;

case (byte)11.3 : System.out.printl ("Eleven");


break;

default : System.out.println("Invalid);

ten
eleven
Invalid
compile time error

ans : compile time error

19.
int a =20;
switch(a){

case (int)10.2 : System.out.println("ten");


break;

case (int)11.3 : System.out.printl ("Eleven");


break;

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

21.What is the specialty of the Scanner class in Java?

A. It is used to perform mathematical operations on input data.


B. It is used to parse and process binary data from a file.
C. It is used to read input from various sources like keyboard, file, and string.
D. It is used to create and manage database connections.

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.

What is the difference between next() and nextLine() in Java?


Options:
A. next() reads a single word and stops at a whitespace, while nextLine() reads an
entire line including spaces.
B. next() reads an entire line including spaces, while nextLine() reads a single
word and stops at a whitespace.
C. next() can only read integers, while nextLine() can read strings.
D. next() is used for numeric input, while nextLine() is used for string input.

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

25.Guess the output for the following code snippet

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?

Yes, it is mandatory to use explicit type casting to avoid data loss.


No, Java automatically handles narrowing without requiring type casting.
Yes, but it is optional if the values are within the range of the target type.
No, narrowing is not allowed 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;

int exp = 4/3 * (x + 34) + 9 * (a + b * c) + (3 + y * (2 + a)) / (a + b*y);

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?"

Why do we need operators in Java?


Options:
To perform logical operations on variables and values.
To store values in variables.
To define the data types of variables.
To organize and control the flow of a program.

ans : To perform logical operations on variables and values.

Q1: What is the correct way to declare a main method in Java?


a) public static void main(String[] args)
b) static public void main(String[] args)
c) public void static main(String[] args)
d) Both a and b
Answer: d) Both a and b

Q2: Which of the following is not a keyword in Java?


a) static
b) void
c) main
d) public
Answer: c) main

Q3: Which of these is not a literal in Java?


a) true
b) null
c) 3.14
d) +
Answer: d) +

Q4: What is required for two methods to be considered overloaded?


a) They must have the same name and same parameters.
b) They must have the same name but different parameter lists.
c) They must have different names and parameter lists.
d) They must have the same return type but different parameter lists.
Answer: b) They must have the same name but different parameter lists.

Q5:What will happen when this code is compiled?


class Test {
void show(int a) { }
void show(int a, int b) { }
int show(int a) { return a; }
}
a) It will compile successfully.
b) It will fail due to duplicate methods.
c) It will fail because overloading is not allowed.
d) It will fail due to a return type mismatch.
Answer: b) It will fail due to duplicate methods.

Q6: Consider the code below. What will happen?


class Test {
static void calculate(int a, double b) {
System.out.println("int,double");
}

static void calculate(double a, int b) {


System.out.println("double,int");
}

public static void main(String[] args) {


calculate(10, 20);
}
}
a) It will compile and print "int-double".
b) It will compile and print "double-int".
c) It will fail at compile-time due to ambiguity.
d) It will fail at runtime due to ambiguity.
Answer: c) It will fail at compile-time due to ambiguity

Q7:What will happen when the following code is executed?


class Test {
static void show(int a) {
System.out.println("Integer: " + a);
}

static void show(float a) {


System.out.println("Float: " + a);
}

public static void main(String[] args) {


show(10);
}
}
a) Compile-time error
b) Prints: Integer: 10
c) Prints: Float: 10.0
d) both b and d

Answer: c) Prints: Integer: 10

Q8: In the context of compile-time binding, what is a method signature?


a) The method name only
b) The method name and its return type
c) The method name and its parameter types
d) The method name, return type, and parameter types

Answer: c) The method name and its parameter types

Q9: What is compile-time binding in Java?


a) Binding that occurs at runtime
b) Binding that is determined when the code is compiled
c) Binding related to method overriding
d) Binding that occurs only with non-static methods

Answer: b) Binding that is determined when the code is compiled

Q10: Which of the following correctly demonstrates a static method call?


a) Example.display();
b) Example obj = new Example(); obj.display();
c) obj.display();
d) display();

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.

Answer: b) It will result in a compile-time error.

Q12:Which two statements are true?


class Foo {
static void alpha()
void beta()
}
A. Foo.beta() is a valid invocation of beta().
B. Foo.alpha() is a valid invocation of alpha().
C. Method beta() can't directly call method alpha().
D. Method alpha() can directly call method beta().
Answer: B

Q13:What is the result?


class Hexy {
public static void main(String[] args) {
int i = 42;
String s = (i<40)?"life":(i>50)?"universe":"everything";
System.out.println(s);
}
}

A. Compilation error.
B. life
C. universe
D. everything

Answer:D

Q14:Q3: If a class in Package A wants to access a member of a class in Package B,


what must be true?
a) The class in Package A does not need to import Package B.
b) The class in Package A must import Package B.
c) The class in Package B must be declared as private.
d) The class in Package A can access the member without any import.

Answer: b) The class in Package A must import Package B

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: d) All of the above

Q16:What will happen if you execute the following code?


public class Example {
public static String concat(String a, String b) {
return a + b;
}

public static void main(String[] args) {


System.out.println(concat(null, "World"));
}
}
a) World
b) nullWorld
c) Compile-time error
d) RuntimeError

Answer: b) nullWorld

Q17: Which of the following statements is true regarding non-primitive variables?


a) Non-primitive variables cannot hold reference.
b) Non-primitive variables store their actual data in the variable itself.
c) Non-primitive variables cannot be null.
d) Non-primitive types include arrays, strings, and classes.

Answer: d) Non-primitive types include arrays, strings, and classes

Q18: Which of the following statements is true about methods in Java?


a) Methods can return multiple values.
b) Methods must be defined inside a class.
c) Methods can have the same name but must have the same parameters.
d) Methods cannot be overloaded.

Answer: b) Methods must be defined inside a class

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: a) Car myCar = new Car();

Q20: What is a static member in a class?


a) A member that belongs to the instance of the class.
b) A member that can only be accessed through an object.
c) A member that can be accessed without creating an instance of the class.
d) A member that cannot be modified.

Answer: c) A member that can be accessed without creating an instance of the class.

Q21: Which of the following can be declared as a member of a class?


a) Methods
b) Variables
c) Intializers
d) All of the above

Answer: d) All of the above

Q22: In which of the following cases can a non-static method be called?


a) Directly from the static context without an object.
b) Only from another non-static method of the same class.
c) From both static and non-static methods using an object.
d) From any static method without creating an object.

Answer: c) From both static and non-static methods using an object.

Q23: What will happen in the following code?


public class Demo{
public static void main(String[] args) {
byte b = 127;
b += 1;
System.out.println(b);
}
}
a) 127
b) 128
c) -128
d) Compilation error

Answer: c) -128

Q23: What will be the output of this code?

public class Demo {


public static void main(String[] args) {
long l = 300;
int i = (int) l;
double d = 12.5;
d = (int) d;
System.out.println(i + d);
}
}
a) 300.0
b) 300
c) 312
d) 300.5

Answer: c) 312

Q24: Consider the following code snippet. What will it print?


public class Demo {
public static void main(String[] args) {
float f = 3.14f;
long l = (long) f;
double d = f;
System.out.println(l + d);
}
}
a) 3.14
b) 6.14
c) 3.0
d) 4.14

Answer: b) 6.14

Q25:What will happen when you run the following code


public class Test {
public static void main(String[] args) {
float f = 4.5f;
int i = (int) f;
f = (float) i + 1.5f;
System.out.println(f);
}
}
a) 6.0
b) 4.5
c) 5.0
d) Compilation error

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?

a) It closes the input stream.


b) It cannot be called on a Scanner object.
c) It resets the Scanner object.
d) It is optional to call in a small program.

Answer: a) It closes the input stream.

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;

Answer: a) import package.className;

Q30.What will be the output of the following code?


public class Test {
public static void main(String[] args) {
int a = 1;
int b = 2;
boolean result = (a++ < 2) && (++b > 2);
System.out.println(b);
}
}
A) 2
B) 3
C) 1
D) Error
Answer: A) 2
Q31.What is the final value of x after the following code is executed?
public class Test {
public static void main(String[] args) {
int x = 0;
x += 5;
x *= 2;
x++;
x -= 4;
System.out.println(x);
}
}
A) 5
B) 7
C) 10
D) 11
Answer: B) 7

Q32 What will be the output of the following Java program?


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

public static int demo(int a) {


System.out.println("Demo Begin");
System.out.println("a:" + a);
System.out.println("Demo End");
return ++a + 3;
}

public static int test(int ch) {


System.out.println("Test Begin");
System.out.println(ch);
System.out.println("Test End");
return ch + 1 + demo(ch);
}
}

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

D) Error: incompatible types: possible lossy conversion from char to int

Q33.What will be the output of the following Java program?


public class Example {
public static void main(String[] args) {
System.out.println(calculate(5));
}

public static int calculate(int number) {


System.out.println("Calculating...");
return number * 2 + increment(number);
}

public static int increment(int value) {


return value + 1;
}
}
A) Calculating...
10

B) Calculating...
11

C) Calculating...
12

D) Error

Answer: C) Calculating...
12

Q34.What will be the output of the following program?


public class Prog2 {
public static void main(String[] args) {
System.out.println(compute(10));
}

public static int compute(int value) {


System.out.println("Compute Start");
System.out.println("value: " + value);
return value + modify(value);
}

public static int modify(int num) {


System.out.println("Modify Start");
System.out.println("num: " + num);
System.out.println("Modify End");
return num - 3;
}
}

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'));
}

public static int check(char c) {


System.out.println("Check Start");
System.out.println("Character: " + c);
System.out.println("Check End");
return (int) c + perform(c);
}

public static int perform(char ch) {


System.out.println("Perform Start");
System.out.println("Character: " + ch);
System.out.println("Perform End");
return (int) ch - 2;
}
}

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

1) What is the output for the following program


class Java
{
public static void main(String[] args)
{
test('a');
}
public static void test(int a)
{
System.out.println("int");
}
public static void test(double d)
{
System.out.println("double");
}
}
a) double
b) int
c) Compile Time Error
d) None of the Above

2) What is the output for the following program


class Java1
{
public static void main(String[] args)
{
String str = "String";
char ch = 'a';
System.out.println(str+ch);
}
}
a) String97
b) Compile Time Error
c) Stringa
d) None of the Above

3) Choose the wrong one

a) return 10>20 ;
b) return 'a'+10;
c) return (int a = 10);
d) return 10>20 ? 10 : 20;

4) What is the output for the following program


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

public static int test(int a)


{
return a=10;
}
}

a) 20
b) compile time error
c) 10
d) None of the Above

5) Who will do the early binding?

a) Interpreter
b) JIT
c) Java Compiler
d) JVM

6) Which one of the following will not done by the compiler

a) Converting source file into class file


b) Importing java.lang package
c) Narrowing
d) Static binding

7) Choose the correct one with respect to Method overlaoding

a) Methods return type should be same


b) Methods should be non static
c) Modifiers should be same
d) None of the Above
8) what is the correct Syntax to import only Scanner class

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?

a) Only static members


b) Only Non static members
c) Only imported members
d) Both static and non static members

10) which one is the correct when the return type of the method is void

a) We can use return with value


b) we can't use return
c) we can use return without data
d) we can use return with expression

11) what is return

a) It is a control transfer statement


b) It is a keyword
c) It will return the value and control
d) All of the Above

12) What is the output for the following program


class Java3
{
public static void main(String[] args)
{
System.out.println(get('a'));
}

public static int get(char ch)


{
return ch;
}
}

a) a
b) 97
c) Compile time error
d) None of the Above

13) Which one we cannot use in the return

a) Value
b) Variable
c) Statement
d) Expression

14) What is actual arguments

a) Variable created in the method declaration


b) Variable created in the method block
c) Value passed in the method calling Statement
d) Value returned by the method

15) Formal arguments are

a) Static variables
b) Local variables
c) non static variables
d) None of the Above

16) What is the output for the following program


class Java4
{
public static void main(String[] args)
{
add(10,20);
}

public static short add(short a,short b)


{
return a+b;
}
}

a) No output
b) Compile time error
c) 30
d) None of the Above

17) Which one is correct increasing order

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

18) What is the output for the following program


class Java5
{
public static void main(String[] args)
{
int a = 10;
test(10+"Perseverence");
}
public static void test(String b)
{
System.out.println("All the best");
}
}

a) No output
b) Complie time error
c) It will not execute
d) All the best

19) Who will do Implicit type casting

a) JVM
b) Java compiler
c) Programmer
d) JIT compiler

20) Which one is correct about Compound assignment operator

a) Type casting
b) Self Updation
c) Reduce code redundancy
d) All of the Above

21) Which one in the following is compile time error

a) true && false


b) ! true == false
c) true || ! false
d) None of the Above

22) What is the output for the following program


class Java6
{
public static void main(String[] args)
{
int a = 5;
System.out.println(3 + a+=5);
}
}

a) 13
b) 8
c) Complie time error
d) None of the Above

23) What is the output for the following program


class Java6
{
public static void main(String[] args)
{
int a=10;
System.out.println( a+(a=a++));
}
}

a) 22
b) 21
c) Complie time error
d) 20

24) What should be the operand for Increment and Decrement operator

a) We can use String data


b) We can use number data directly
c) We can use boolean data
d) We can use only variable

25) What is the output for the following program


class Java7
{
public static void main(String[] args)
{
int a = 20;
int res = a<20 ? a : 'a';
System.out.println(res);
}
}

a) Complie time error


b) 20
c) 97
d) 'a'

26) What we cannot use in the place of Operand2 and Operand3 in Conditional
Operator

a) Expression
b) Statement
c) data
d) Variable

27) Which one of the following is Complie time Success

a) "Hii" >= "Hii"


b) true > false
c) 'a'>=97
d) "Hello" != true

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

29) Which one is not the behavior of the Operator in java

a) Assigning the data to the variable


b) Performing Type casting
c) Updating the variable
d) Declare a Variable

30) Which operator will have polymorphic nature

a) &&
b) ++
c) +
d) %

31) Which operator will have Rigth to Left associativity

a) Dot(.) Operator
b) Compound Assignment operators
c) Relational operators
d) Arithmetic Operators

32) Which member that we can access by using Dot(.) Operator


a) Class members
b) Objcet members
c) Both (a) and (b)
d) None of the Above

33) What is the output for the following program


class Java8
{
public static void main(String[] args)
{
{
int a = 20;
}
int a= 30;
System.out.println(a);
}
}

a) Run time error


b) 30
c) 20
d) Complie time error

34) In main method (public static void main(String[] args) ) args is

a) keyword
b) identifier
c) Literal
d) Symbol

35) What is keyword

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

1.What is the primary purpose of the Java Virtual Machine (JVM)?

a) Compiles Java code into bytecode

b) Executes bytecode and provides a runtime environment

c) Provides an IDE for Java development

d) Translates bytecode into C code

Answer: b) Executes bytecode and provides a runtime environment


Reason: The JVM is responsible for executing the compiled bytecode, making Java
platform-independent.

2. Which method is used to compare two strings in Java?

a) equals()
b) compareTo()

c) ==

d) compare()

Answer: a) equals()
Reason: The equals() method is used to compare the content of two strings.

3.What will be the output of the following code snippet?


int x = 5;
int y = ++x;
System.out.println(y);

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.

4.Which of the following is NOT a valid access modifier in Java?

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.

5.What is the default access level in Java?

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

7.Which of the following is a correct method declaration in Java?

A) public void method1() { }


B) public method1() void { }
C) method1 void() { public }
D) void public method1() { }

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;

9.What is the role of the method body in Java?

A) It defines the method's signature.


B) It contains the logic and instructions to execute when the method is called.
C) It specifies the method's return type.
D) It determines the method's name

10.Which of the following is true about access modifiers in Java methods?

A) Private methods can only be called within the same class.


B) Protected methods can be accessed only from the same package.
C) Public methods are accessible only from the same package.
D) Default methods can be accessed only from other classes in the same package.

11.Which statement is true about formal parameters/formal arguments in Java


methods?

A) Formal parameters must always be passed by reference.


B) Formal parameters are used to define the method's return type.
C) Formal parameters are defined in the method declaration and are used in the
method body.
D) Formal parameters are only necessary if the method uses them in its body.

12.Guess the output for the following code snippet.


int x = 2, y = 5;
int exp1 = (x * y / x);
int exp2 = (x * (y / x));
System.out.println(exp1);
System.out.println(exp2);

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

14.Guess the output for the following code snippet.

int x = 20, y = 30, z = 50;


x += y; // x = x + y
y -= x + z;
z *= x * y;
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);

A) x = 50, y = -50, z = -2500


B) x = 50, y = -70, z = -175000
C) x = 50, y = 70, z = 25000
D) x = 70, y = -50, z = 15000

Answer: A) x = 50, y = -70, z = -175000

15.Guess the output for the following code snippet.

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

18.Guess the output for the following code snippet.

int a = 20, b = 10;


boolean c = true, d = false;
a = c ? b++ : b--;
c = !d;
System.out.print((a + b) + " " + (c ? 5 : 10));

A) 21 5
B) 15 10
C) 30 5
D) 20 5
Answer : A)21 5

19.Which of the following is a valid declaration of an integer variable in Java?

A) int 1x = 10;
B) int x1 = 10;
C) int x 10;
D) int = 10 x;
Answer: B) int x1 = 10;

20.Guess the output for the following code

float f = 5f;
System.out.println(f);

A) 5
B) 5.00
C) 5.0000
D) 5.0

Answer D) 5.0

21.What is the result of the following operation in Java?

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

22.Guess the output for the following code snippet


int a = 10;
int b = 20;
a= a+=b+=b+=b+=b;
System.out.println(a);

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

25.What will be the output for the following code snippet?


String s = "Java";
System.out.println(5 + 10 + s + 5 + 10);

A) 15Java510
B) 15Java15
C) 15Java510
D) Compile-time error

Answer A) 15Java510

26.Guess the output for the following code

class Test {
private static void display() {
System.out.println("Hello");
}

public static void main(String[] args) {


display();
}
}

A) The code will compile and print "Hello".


B) The code will compile but result in a runtime exception.
C) The code will not compile because of access modifier restrictions.
D) The code will compile but will give an error when calling display().

Answer: A) The code will compile and print "Hello".

27.Guess the output for the following code


class Demo {
public void display() {
System.out.println("Non-static method called");
}

public static void main(String[] args) {


Demo.display();
}
}
A) Non-static method called
B) Compile-time error
C) Runtime exception
D) Nothing will be printed

Answer B) Compile-time error


28.Guess the output for the following code
class Demo {
public static void main(String[] args) {
System.out.println(Math.pow(2, 3));
}
}
A) 8.000
B) 8.0
C) 8
D) 8.0f

Answer : B) 8.0

29. class Demo {


public static void main(String[] args) {
System.out.println(Math.floor(5.8));
}
}
A) 6.0
B) 5.0
C) 5
D) 6

30.

Which of the following expressions has the highest precedence?

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?

A) They operate on a single operand.


B) They operate on two operands.
C) They are used for logical negation.
D) They cannot be used with non-primitive types.

Answer : B) They operate on two operands

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

Answer : C) A sequence of operators and operands that evaluates to a value

34.What is an operand in Java?

A) An operator used to perform a calculation


B) A data type that holds a value
C) A value or variable that an operator acts on
D) A method that returns a value

Answer: C) A value or variable that an operator acts on

35.What is ASCII?

A) A programming language used to write system software


B) A character encoding standard that represents text in computers
C) A data structure used to store text
D) A protocol used for data transmission between systems

Answer: B) A character encoding standard that represents text in computers

1. Which of the following is NOT a characteristic of a compiled language? a) Faster


execution b) Platform-dependent c) Easier debugging d) Better portability
2. What is the role of a compiler? a) Executes the program directly b) Translates
high-level code to machine code c) Interprets the code line by line d) Creates an
interactive environment for the programmer
3. Which of the following is the correct order of components in a Java program
structure? a) Class, Method, Variables, Statements b) Method, Class, Variables,
Statements c) Variables, Class, Method, Statements d) Statements, Class, Method,
Variables
4. What makes Java "Platform Independent"? a) It runs directly on the hardware b)
It is compiled into machine code specific to the operating system c) It generates
bytecode that runs on the Java Virtual Machine (JVM) d) It is written in a high-
level language
5. What is the relationship between JDK, JRE, and JVM? a) JDK contains JRE, which
contains JVM b) JRE contains JDK, which contains JVM c) JVM contains JRE, which
contains JDK d) JDK and JRE are the same, both containing JVM
6. Which of the following steps is NOT typically involved in compiling and
executing a Java program? a) Writing the Java source code (.java) b) Compiling the
source code using the Java compiler (javac) c) Executing the compiled bytecode
(.class) using the Java Virtual Machine (java) d) Linking the compiled code with
external libraries
7. Which statement is used to print data to the console in Java? a)
System.out.println(); b) Console.print(); c) Display.show(); d) Output.write();
8. What is the purpose of the main() method in Java? a) It is the entry point for
program execution b) It is used to define global variables c) It is responsible for
handling user input d) It is used for creating objects
9. What is the output of the following code snippet?
Java
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
a) Hello, World! b) HelloWorld c) main d) No output
10. What is a local variable in Java? a) A variable declared within a method or
block b) A variable declared outside of any method c) A variable that can be
accessed from anywhere in the program d) A variable that is automatically
initialized to zero
11. What is a datatype in Java? a) A set of values that a variable can hold b) The
name given to a variable c) The size of the memory allocated to a variable d) The
way a variable is stored in memory
12. Which of the following is NOT a primitive datatype in Java? a) int b) float c)
String d) boolean
13. Which of the following datatypes is used to represent a single character in
Java? a) char b) string c) byte d) short
14. Which operator is used to perform integer division in Java? a) / b) % c) *
d) //
15. What is the result of the following expression: 5 + 2 * 3 a) 7 b) 11 c) 15 d)
21
16. What is the purpose of the conditional operator (ternary operator)? a) To
perform arithmetic operations b) To compare two values c) To execute different code
blocks based on a condition d) To create loops
17. What is primitive type casting in Java? a) Converting a variable from one
primitive datatype to another b) Creating a new object from an existing object c)
Changing the access modifier of a method d) Passing arguments to a method
18. Which of the following is an example of primitive type casting? a) int x =
(int) 3.14; b) String s = Integer.toString(10); c) Object obj = new Integer(5); d)
double d = Math.sqrt(4);
19. What is a parameterized method in Java? a) A method that does not accept any
arguments b) A method that accepts arguments c) A method that returns a value d) A
method that does not return a value
20. What is a non-parameterized method in Java? a) A method that accepts arguments
b) A method that does not accept any arguments c) A method that returns a value d)
A method that does not return a value
21. Which keyword is used to define a method in Java? a) class b) void c) public d)
static
22. Which keyword is used to return a value from a method in Java? a) return b)
void c) this d) new
23. What is the output of the following code snippet?
Java
public class Test {
public static int add(int a, int b) {
return a + b;
}

public static void main(String[] args) {


int result = add(5, 3);
System.out.println(result);
}
}
a) 5 b) 3 c) 8 d) 15
24. What is method overloading?
a) Creating multiple methods with the same name but different return types. b)
Creating multiple methods with the same name and the same parameters. c) Creating
multiple methods with the same name but different parameters. d) Creating multiple
methods with different names and different parameters.
25. Which of the following is a valid example of method overloading?
a)
Java
void myMethod() {
// ...
}

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) {
// ...
}

void myMethod(int x, int y) {


// ...
}
26. What is the significance of method overloading?
a) To improve code readability and maintainability. b) To reduce code redundancy.
c) To allow for more flexible method usage. d) All of the above.
27. Which of the following is NOT a valid way to overload a method?
a) Changing the number of parameters. b) Changing the order of parameters. c)
Changing the data types of parameters. d) Changing only the return type of the
method.
28. What happens if you try to overload a method with the same number and type of
parameters but different return types?
a) The compiler will generate an error. b) The compiler will choose the method
based on the return type. c) The compiler will choose the method based on the order
of method declaration. d) The program will execute without any issues.
29. Which operator is used for integer division in Java?
• a) /
• b) %
• c) *
• d) //
30. What is the result of the expression 7 % 3?
• a) 2
• b) 2.33
• c) 3
• d) 0
31. Which operator is used for logical AND?
• a) &&
• b) ||
• c) !
• d) ^
32. Which operator is used for logical OR?
• a) &&
• b) ||
• c) !
• d) ^
33. What is the result of the expression true && false?
• a) true
• b) false
• c) undefined
• d) null
34. What is the result of the expression true || false?
• a) true
• b) false
• c) undefined
• d) null
35. What is the result of the expression 5 | 3?
• a) 1
• b) 7
• c) 0
• d) CTE

You might also like