[go: up one dir, main page]

0% found this document useful (0 votes)
6 views34 pages

Java Question Bank

The document covers Java fundamentals, including its features, the System class, the main method, and operators. It discusses concepts such as platform independence, OOP principles, and memory management, as well as providing detailed explanations of various operators and their usage. Additionally, it includes programming exercises and questions related to Java syntax and behavior.

Uploaded by

Riya Yohannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views34 pages

Java Question Bank

The document covers Java fundamentals, including its features, the System class, the main method, and operators. It discusses concepts such as platform independence, OOP principles, and memory management, as well as providing detailed explanations of various operators and their usage. Additionally, it includes programming exercises and questions related to Java syntax and behavior.

Uploaded by

Riya Yohannan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Java Fundamentals

Features of Java:
1. What are the main features of Java that distinguish it from other programming
languages?
2. Explain the concept of platform independence in Java.
3. How does Java support object-oriented programming (OOP) principles?
4. Discuss the importance of Java's automatic memory management (garbage
collection).
5. How does Java achieve robustness in its programming language design?
System Class:
1. What is the System class in Java, and what are its main functions?
2. How do you use the System.out.println() method to print output in Java?
3. Explain the difference between System.out.print() and System.out.println().
4. How can you use the System.arraycopy() method to copy arrays in Java?
5. Discuss the role of the System class in managing system properties and environment
variables in Java.
6. Explain System.out.println()
Main Method:
1. What is the significance of the main method in a Java program?
2. What is the signature of the main method in Java?
3. Can you have multiple main methods in a single Java class? Why or why not?
4. Explain the purpose of the args parameter in the main method.
5. How do you run a Java program from the command line, specifying command-line
arguments?
6. Explain public static void main(String args[])

Operators

1. What is the purpose of operators in Java?


2. How are operators classified in Java?
3. Explain the difference between unary, binary, and ternary operators.
4. What is the significance of the "+" operator in Java?
5. How does the "==" operator differ from the "equals()" method in Java?
6. What is the difference between the "&&" and "||" operators in Java?
7. How does the ternary operator (?:) work in Java?
8. Explain the concept of operator precedence in Java.
9. What are the bitwise operators in Java? Provide examples.
10. How are arithmetic operators used in Java? Provide examples.
11. What are the assignment operators in Java? Give examples.
12. Explain the difference between prefix and postfix increment operators (++x and x++).
13. How do relational operators work in Java? Give examples.
14. What is the purpose of the instanceof operator in Java?
15. How do the bitwise shift operators (<<, >>, >>>) work in Java?
16. Explain the concept of short-circuiting in logical operators.
17. How are compound assignment operators different from simple assignment
operators?
18. What are the unary operators available in Java? Provide examples.

www.vertexitservices.com Call On: +91 9763 9763 33


19. Explain the use of the conditional operator in Java.
20. How do the arithmetic compound assignment operators work in Java?
21. What are the logical operators available in Java? Provide examples.
22. How do you use the instanceof operator in Java? Provide an example.
23. How do you use the ternary operator in Java? Provide an example.
24. How do you use the bitwise operators in Java? Provide an example.
25. How do you use the arithmetic operators in Java? Provide an example.

Find the Output


1. Which of the following can be operands of arithmetic operators?
a. Numeric
b. Boolean
c. Characters
d. Both Numeric & Characters
2. Modulus operator, %, can be applied to which of these?
a. Integers
b. Floating – point numbers
c. Both Integers and floating – point numbers.
d. None of the mentioned
3. With x = 0, which of the following are legal lines of Java code for changing the value
of x to 1?
i. x++;
ii. x = x + 1;
iii. x += 1;
iv. x =+ 1;
a. i, ii & iii
b. i & iv
c. i,ii,iii & iv
d. iii & ii
4. Decrement operator, −−, decreases value of variable by what number?
a. 1
b. 2
c. 3
d. 4
5. Which of these statements are incorrect?
a. Assignment operators are more efficiently implemented by Java run-time
system than their equivalent long forms.
b. Assignment operators run faster than their equivalent long forms.
c. Assignment operators can be used only with numeric and character data type.
d. None

6. What is the output of this program?


class increment
{
public static void main(String args[])
{
double var1 = 1 + 5;
double var2 = var1 / 4;
int var3 = 1 + 5;

www.vertexitservices.com Call On: +91 9763 9763 33


int var4 = var3 / 4;
System.out.print(var2 + " " + var4);
}
}
a. 11
b. 01
c. 1.5 1
d. 1.5 1.0

7. What is the output of this program?

class Modulus {
public static void main(String args[])
{
double a = 25.64;
int b = 25;
a = a % 10;
b = b % 10;
System.out.println(a + " " + b);
}
}
a. 5.640000000000001 5
b. 5.640000000000001 5.0
c. 5 5
d. 5 5.640000000000001

8. What is the output of this program?

class increment {
public static void main(String args[])
{
int g = 3;
System.out.print(++g * 8);
}
}
a. 25
b. 24
c. 32
d. 33
9. Can 8 byte long data type be automatically type cast to 4 byte float data type?
a. True
b. False

10. What is the output of this program?


class Output {
public static void main(String args[])
{
int a = 1;

www.vertexitservices.com Call On: +91 9763 9763 33


int b = 2;
int c;
int d;
c = ++b;
d = a++;
c++;
b++;
++a;
System.out.println(a + " " + b + " " + c);
}
}
a. 324
b. 323
c. 234
d. 344

11. Which of these have highest precedence?


a. ()
b.++
c. *
d.>>

12. What should be expression1 evaluate to in using ternary operator as in this line?
a. expression1 ? expression2 : expression3
a. Integer
b. Floating – point numbers
c. Boolean
d. None of the mentioned

13. What is the value stored in x in following lines of code?


int x, y, z;
x = 0;
y = 1;
x = y = z = 8;
a. 0
b.1
c. 9
d.8

14. What is the order of precedence (highest to lowest) of following operators?


&
^
?:
a. 1 -> 2 -> 3
b.2 -> 1 -> 3
c. 3 -> 2 -> 1
d.2 -> 3 -> 1

15. Which of these statements are incorrect?

www.vertexitservices.com Call On: +91 9763 9763 33


a. Equal to operator has least precedence.
b.Brackets () have highest precedence.
c. Division operator, /, has higher precedence than multiplication operator.
d.Addition operator, +, and subtraction operator have equal precedence.

16. What is the output of this program?

a. class operators {
public static void main(String args[])
{
int var1 = 5;
int var2 = 6;
int var3;
var3 = ++ var2 * var1 / var2 + var2;
System.out.print(var3);
}
}
a. 10
b.11
c. 12
d.56

17. What is the output of this program?

class operators {
public static void main(String args[])
{
int x = 8;
System.out.println(++x * 3 + " " + x);
}
}
a. 24 8
b.24 9
c. 27 8
d.27 9

18. What is the output of this program?

class Output {
public static void main(String args[])
{
int x=y=z=20;

}
}
a. compile and runs fine
b. 20
c. run time error
d. compile time error

www.vertexitservices.com Call On: +91 9763 9763 33


19. Which of these lines of code will give better performance?
1. a | 4 + c >> b & 7;
2. (a | ((( 4 * c ) >> b ) & 7 ))
a. 1 will give better performance as it has no parentheses.
b.2 will give better performance as it has parentheses.
c. Both 1 & 2 will give equal performance.
d.Dependent on the computer system.

20. What is the output of this program?

a. class Output {
public static void main(String args[])
{
int a,b,c,d;
a=b=c=d=20
a+=b-=c*=d/=20
System.out.println(a+" "+b+" "+c+" "+d);

}
}
a. compile time error
b. runtime error
c. a=20 b=0 c=20 d=1
d. none of the mentioned

21. Which of the following are not unary expressions?


a. a+b
b. ++a
c. a+=1

22. Which of the following are binary expressions?


a. a+b
b. ++a
c. a+=1
d. a++

23. If originally x=10, what is the value of x after each of the expressions:
a. ++x
b. x++
24. If x=10 and sum=0, what is the value of sum after each of the expressions:
a. sum=++x
b. sum=x++
25. If originally x=10 what is the value of each of the expressions:
a. --x
b. x--
26. If originally x=10 and sub=0 what is the value of sub after each of the expressions:
a. sub=--x
b. sub=x--

www.vertexitservices.com Call On: +91 9763 9763 33


27. If originally x=10 and y=5 what is the value of each of the expressions:
a. ++x+y
b. --x+y
c. --x+(++y)
d. --x+y--

28. In the following class definition, which is the first line (if any) that causes a compilation
error. Select the one correct answer.

public class test {


public static void main(String args[]) {
1. char c;
2. int i;
3. c = 'A'; // 1
4. i = c; //2
5. c = i + 1; //3
6. c++; //4
}
}
29. public class test {
public static void main(String args[]) {
char c;
int i;
c = 'A'; // 1
i = c; //2
c = i + 1; //3
c++; //4
}
}
a. The line labeled 1.
b. The line labeled 2.
c. The line labeled 3.
d. The line labeled 4.
e. All the lines are correct and the program compiles.
30. Which of these assignments are valid. Select the four correct answers.
a. short s = 28;
b. float f = 2.3;
c. double d = 2.3;
d. int I = ‘1’;
e. byte b = 12;
31. What gets printed when the following program is compiled and run. Select the one
correct answer.

class test {
public static void main(String args[]) {
1. int i,j,k,l=0;
2. k = l++;
3. j = ++k;
4. i = j++;

www.vertexitservices.com Call On: +91 9763 9763 33


5. System.out.println(i);
}
}
32. What gets printed when the following program is compiled and run. Select the one
correct answer
class test {
public static void main(String args[]) {
1. int i,j,k,l=0;
2. k = l++;
3. j = ++k;
4. i = j++;
5. System.out.println(i);
}
}
a. 0
b. 1
c. 2
d. 3
33. Which of these lines will compile? Select the four correct answers.
a. short s = 20;
b. byte b = 128;
c. char c = 32;
d. double d = 1.4;;
e. float f = 1.4;
f. byte e = 0;
34. The signed right shift operator in Java is –. Select the one correct answer.
a. <<;
b. >>
c. >>>;
d. None of these.
35. What gets printed on the standard output when the class below is compiled and
executed. Select the one correct answer.

public class ShortCkt {


public static void main(String args[]) {
1. int i = 0;
2. boolean t = true;
3. boolean f = false, b;
4. b = (t && ((i++) == 0));
5. b = (f && ((i+=2) > 0));
6. System.out.println(i);
}
}
36. What gets printed when the following program is compiled and run. Select the one
correct answer
public class ShortCkt {
public static void main(String args[]) {
1. int i = 0;
2. boolean t = true;
3. boolean f = false, b;

www.vertexitservices.com Call On: +91 9763 9763 33


4. b = (t && ((i++) == 0));
5. b = (f && ((i+=2) > 0));
6. System.out.println(i);
}
}
37. What gets printed on the standard output when the class below is compiled and
executed. Select the one correct answer.

public class ShortCkt {


public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
System.out.println(i);
}
}
38. What gets printed when the following program is compiled and run. Select the one
correct answer
public class ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
System.out.println(i);
}
39. What gets printed on the standard output when the class below is compiled and
executed. Select the one correct answer.

public class ShortCkt {


public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
}
}
40. What gets printed when the following program is compiled and run. Select the one
correct answer
public class ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;

www.vertexitservices.com Call On: +91 9763 9763 33


boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
}
}
41. What gets printed on the standard output when the class below is compiled and
executed. Select the one correct answer.

public class ShortCkt {


public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t | ((i++) == 0));
b = (f | ((i+=2) > 0));
System.out.println(i);
}
}
42. What gets printed on the standard output when the class below is compiled and
executed.
public class ShortCkt {
public static void main(String args[]) {
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t | ((i++) == 0));
b = (f | ((i+=2) > 0));
System.out.println(i);
}
}
43. Which operator is used to perform bitwise inversion in Java. Select the one correct
answer.
a. ~
b. !
c. &
d. |
e. ^
44. What gets printed when the following program is compiled and run..

public class test {


public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}
45. What gets printed when the following program is compiled and run

www.vertexitservices.com Call On: +91 9763 9763 33


public class test {
public static void main(String args[]) {
byte x = 3;
x = (byte)~x;
System.out.println(x);
}
}
a. 3
b. 0
c. 1
d. 11
e. 252
f. 214
g. 124
h. -4
46. What gets displayed on the screen when the following program is compiled and run.
public class test {
public static void main(String args[]) {
int x,y;
x = 3 & 5;
y = 3 | 5;
System.out.println(x + " " + y);
}
}
47. What gets displayed on the screen when the following program is compiled and run
public class test {
i. public static void main(String args[]) {
1. int x,y;
2. x = 3 & 5;
3. y = 3 | 5;
4. System.out.println(x + " " + y);
ii. }
}
a. 7 1
b. 3 7
c. 1 7
d. 3 1
e. 1 3
f. 7 3
g. 7 5
48. What gets displayed on the screen when the following program is compiled and run.

public class test {


public static void main(String args[]) {
int x,y;
x = 1 & 7;
y = 3 ^ 6;
System.out.println(x + " " + y);
}

www.vertexitservices.com Call On: +91 9763 9763 33


}
49. What gets displayed on the screen when the following program is compiled and run
public class test {
public static void main(String args[]) {
int x,y;
x = 1 & 7;
y = 3 ^ 6;
System.out.println(x + " " + y);
}
}
a. 1 3
b. 3 5
c. 5 1
d. 3 6
e. 1 7
f. 1 5
50. Which operator is used to perform bitwise exclusive or.
a. &
b. ^
c. |
d. !
e. ~
51. What gets displayed on the screen when the following program is compiled and run.
Select the one correct answer.
public class test {
public static void main(String args[]) {
boolean x = true;
int a;
if(x) a = x ? 1: 2;
else a = x ? 3: 4;
System.out.println(a);
}
}
52. What gets displayed on the screen when the following program is compiled and run
public class test {
public static void main(String args[]) {
boolean x = true;
int a;
if(x) a = x ? 1: 2;
else a = x ? 3: 4;
System.out.println(a);
}
}
53. What gets displayed on the screen when the following program is compiled and run.
public class test {
public static void main(String args[]) {
boolean x = false;
int a;

www.vertexitservices.com Call On: +91 9763 9763 33


if(x) a = x ? 1: 2;
else a = x ? 3: 4;
System.out.println(a);
}
}
54. What gets displayed on the screen when the following program is compiled and run
public class test {
public static void main(String args[]) {
boolean x = false;
int a;
if(x) a = x ? 1: 2;
else a = x ? 3: 4;
System.out.println(a);
}
}
55. What gets displayed on the screen when the following program is compiled and run.
public class test {
public static void main(String args[]) {
int x, y;

x = 5 >> 2;
y = x >>> 2;
System.out.println(y);
}
}
56. What gets displayed on the screen when the following program is compiled and run.
public class test {
public static void main(String args[]) {
int x, y;

x = 5 >> 2;
y = x >>> 2;
System.out.println(y);
}
}
a. 5
b. 2
c. 80
d. 0
e. 64
57. What gets displayed on the screen when the following program is compiled and run.
public class test {
public static void main(String args[]) {
int x;

x = -3 >> 1;
x = x >>> 2;
x = x << 1;

www.vertexitservices.com Call On: +91 9763 9763 33


System.out.println(x);
}
}
58. What gets displayed on the screen when the following program is compiled and run
public class test {
public static void main(String args[]) {
int x;

x = -3 >> 1;
x = x >>> 2;
x = x << 1;
System.out.println(x);
}
}
a. 1
b. 0
c. 7
d. 5
e. 23
f. 2147483646
59. Which of the following are correct. Select all correct answers.
a. Java provides two operators to do left shift – << and <<<.
b. >> is the zero fill right shift operator.
c. >>> is the signed right shift operator.
d. For positive numbers, results of operators >> and >>> are same.
60. What is the result of compiling and running the following program.
public class test {
public static void main(String args[]) {
int i = -1;
i = i >> 1;
System.out.println(i);
}
}
61. What is the result of compiling and running the following program
public class test {
public static void main(String args[]) {
int i = -1;
i = i >> 1;
System.out.println(i);
}
}
a. 63
b. -1
c. 0
d. 1
e. 127
f. 128
g. 255
62. What all gets printed when the following gets compiled and run.

www.vertexitservices.com Call On: +91 9763 9763 33


public class example {
public static void main(String args[]) {
int x = 0;
if(x > 0) x = 1;

switch(x) {
case 1: System.out.println(1);
case 0: System.out.println(0);
case 2: System.out.println(2);
break;
case 3: System.out.println(3);
default: System.out.println(4);
break;
}
}
}
63. What all gets printed when the following gets compiled and run
public class example {
public static void main(String args[]) {
int x = 0;
if(x > 0) x = 1;

switch(x) {
case 1: System.out.println(1);
case 0: System.out.println(0);
case 2: System.out.println(2);
break;
case 3: System.out.println(3);
default: System.out.println(4);
break;
}
}
}
64. What happens when the following class is compiled and run..

public class test {


public static void main(String args[]) {
int x = 0, y = 1, z;
if(x)
z = 0;
else
z = 1;

if(y)
z = 2;
else
z = 3;

www.vertexitservices.com Call On: +91 9763 9763 33


System.out.println(z);
}
}
65. What happens when the following class is compiled and run.
public class test {
public static void main(String args[]) {
int x = 0, y = 1, z;
if(x)
z = 0;
else
z = 1;

if(y)
z = 2;
else
z = 3;
System.out.println(z);
}
}
a. The program prints 0
b. The program prints 1
c. The program prints 2
d. The program prints 3
e. The program does not compile because of problems in the if statement.
66. Which all lines are part of the output when the following code is compiled and run.
public class test {
public static void main(String args[]) {
for(int i = 0; i < 3; i++) {
for(int j = 3; j >= 0; j--) {
if(i == j) continue;
System.out.println(i + " " + j);
}
}
}
}
67. Which all lines are part of the output when the following code is compiled and run
public class test {
public static void main(String args[]) {
for(int i = 0; i < 3; i++) {
for(int j = 3; j >= 0; j--) {
if(i == j) continue;
System.out.println(i + " " + j);
}
}
}
}
a. 0 0
b. 0 1

www.vertexitservices.com Call On: +91 9763 9763 33


c. 0 2
d. 0 3
e. 1 0
f. 1 1
g. 1 2
h. 1 3
i. 2 0
j. 2 1
k. 2 2
l. 2 3
m. 3 0
n. 3 1
o. 3 2
p. 3 3
q. The program does not print anything.
68. Which all lines are part of the output when the following code is compiled and run.
public class test {
public static void main(String args[]) {
for(int i = 0; i < 3; i++) {
for(int j = 3; j <= 0; j--) {
if(i == j) continue;
System.out.println(i + " " + j);
}
}
}
}
69. Which all lines are part of the output when the following code is compiled and run.
public class test {
i. public static void main(String args[]) {
ii. for(int i = 0; i < 3; i++) {
1. for(int j = 3; j <= 0; j--) {
2. if(i == j) continue;
3. System.out.println(i + " " + j);
4. }
iii. }
iv. }
}
a. 0 0
b. 0 1
c. 0 2
d. 0 3
e. 1 0
f. 1 1
g. 1 2
h. 1 3
i. 2 0
j. 2 1
k. 2 2
l. 2 3

www.vertexitservices.com Call On: +91 9763 9763 33


m. 3 0
n. 3 1
o. 3 2
p. 3 3
q. The program does not print anything.
70. Which all lines are part of the output when the following code is compiled and run.
public class test {
public static void main(String args[]) {
for(int i = 0; i < 3; i++) {
for(int j = 3; j >= 0; j--) {
if(i == j) break;
System.out.println(i + " " + j);
}
}
}
}
71. Which all lines are part of the output when the following code is compiled and run.
public class test {
i. public static void main(String args[]) {
ii. for(int i = 0; i < 3; i++) {
1. for(int j = 3; j >= 0; j--) {
2. if(i == j) break;
3. System.out.println(i + " " + j);
4. }
iii. }
iv. }
}
a. 0 0
b. 0 1
c. 0 2
d. 0 3
e. 1 0
f. 1 1
g. 1 2
h. 1 3
i. 2 0
j. 2 1
k. 2 2
l. 2 3
m. 3 0
n. 3 1
o. 3 2
p. 3 3
72. Which all lines are part of the output when the following code is compiled and run.

public class test {


public static void main(String args[]) {
outer: for(int i = 0; i < 3; i++) {
for(int j = 3; j >= 0; j--) {

www.vertexitservices.com Call On: +91 9763 9763 33


if(i == j) continue outer;
System.out.println(i + " " + j);
}
}
}
}
73. Which all lines are part of the output when the following code is compiled and run
public class test {
public static void main(String args[]) {
outer: for(int i = 0; i < 3; i++) {
for(int j = 3; j >= 0; j--) {
if(i == j) continue outer;
System.out.println(i + " " + j);
}
}
}
}
a. 0 0
b. 0 1
c. 0 2
d. 0 3
e. 1 0
f. 1 1
g. 1 2
h. 1 3
i. 2 0
j. 2 1
k. 2 2
l. 2 3
m. 3 0
n. 3 1
o. 3 2
p. 3 3
74. Which all lines are part of the output when the following code is compiled and run.
public class test {
public static void main(String args[]) {
outer : for(int i = 0; i < 3; i++) {
for(int j = 3; j >= 0; j--) {
if(i == j) break outer;
System.out.println(i + " " + j);
}
}
}
}
75. Which all lines are part of the output when the following code is compiled and run.
public class test {
public static void main(String args[]) {
outer : for(int i = 0; i < 3; i++) {

www.vertexitservices.com Call On: +91 9763 9763 33


for(int j = 3; j >= 0; j--) {
if(i == j) break outer;
System.out.println(i + " " + j);
}
}
}
}
a. 00
b. 01
c. 02
d. 03
e. 10
f. 11
g. 12
h. 13
i. 20
j. 21
k. 22
l. 23
m. 30
n. 31
o. 32
p. 33

76. int x = 0, y = 0 , z = 0 ;
x = (++x + y-- ) * z++;
What will be the value of "x" after execution ?
a. -2
b. -1
c. 0
d. 1
e. 2
77. int ++a = 100 ;
System.out.println( ++a ) ;
What will be the output of the above fraction of code ?
a. 100
b. Displays error as ++a is not enclosed in double quotes in println statement
c. Compiler displays error as ++a is not a valid identifier
d. None of these
78. What is the output of the following program ?
class Numbers{
public static void main(String args[]){
int a=20, b=10;
if((a < b) && (b++ < 25)){
System.out.println("This is any language logic");
}
System.out.println(b);
}
}

www.vertexitservices.com Call On: +91 9763 9763 33


a. 12
b. 11
c. 10
d. Compilation Error
79. Select from among the following character escape code which is not available in Java.
a. \t
b. \r
c. \a
d. \\
e. \v
80. What will be the output?
if(1 + 1 + 1 + 1 + 1 == 5){
System.out.print("TRUE");
}
else{
System.out.print("FLASE");
}
a. TRUE
b. FALSE
c. Compiler Error
d. None of these

81. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

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

}
}

82. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

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

www.vertexitservices.com Call On: +91 9763 9763 33


}
}

83. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= ++a + 1;

System.out.println(a);

}
}

84. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= a++ + 1;

System.out.println(a);

}
}

85. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= ++a + ++a;

System.out.println(a);

}
www.vertexitservices.com Call On: +91 9763 9763 33
}

86. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= a++ + a++;

System.out.println(a);

}
}

87. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= a++ + ++a;

System.out.println(a);

}
}

88. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a, b;
a=10;
b=--a;
System.out.println(b);

}
}

www.vertexitservices.com Call On: +91 9763 9763 33


89. What is the output of the following code?
package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a, b;
a=30;
b=a--;
System.out.println(a);

}
}

90. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= --a - 1;

System.out.println(a);

}
}

91. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= a-- - 1;

System.out.println(a);

}
}

92. What is the output of the following code?


package com.instanceofjava;

www.vertexitservices.com Call On: +91 9763 9763 33


class IncrementDemo{

public static void main(String [] args){

int a=20;
a= --a - --a;

System.out.println(a);

}
}
93. What is the output of the following code?
package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= a-- - a--;

System.out.println(a);

}
}

94. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=20;
a= a-- - --a;

System.out.println(a);

}
}

95. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

www.vertexitservices.com Call On: +91 9763 9763 33


int a=20;
a= a-- + ++a;

System.out.println(a);

}
}

96. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=1;

System.out.println(a++);
System.out.println(a++);
System.out.println(++a);

System.out.println(a++);
System.out.println(a++);

System.out.println(a--);
System.out.println(a--);

System.out.println(--a);
System.out.println(--a);
System.out.println(a--);

}
}

97. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int i=15;
System.out.println(i++);
System.out.println(i--);

}
}

www.vertexitservices.com Call On: +91 9763 9763 33


98. What is the output of the following code?
package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int a=10,b=10;

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

if(++a>2||++b>2){
a++;
}

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

}
}

99. What is the output of the following code?


package com.instanceofjava;

class IncrementDemo{

public static void main(String [] args){

int i;

for( i=1;i<4;i++){

System.out.println(i);

}
System.out.println("value of i after completion of loop: "+i);

}
}

100. What is the output of the following code?


package com.instanceofjava;

public class A{

static int a = 1111;


www.vertexitservices.com Call On: +91 9763 9763 33
static
{
a = a-- - --a;
}

{
a = a++ + ++a;
}

101. What is the output of the following code?


public static void main(String[] args) {

System.out.println(a);

Control Structures
1. What is a control structure in programming, and why is it important?
2. Explain the difference between sequential, selection, and iteration control structures.
3. How does the if statement work as a selection control structure in programming?
4. What is the purpose of the switch statement in programming, and how does it differ
from if-else statements?
5. Discuss the concept of loops as iteration control structures in programming. Provide
examples of different types of loops.
6. How does the break statement affect the flow of control in a loop or switch
statement?
7. Explain the concept of nesting control structures in programming. Provide an example.
8. Discuss the role of the continue statement in controlling loop execution.
9. How can you use the for-each loop (enhanced for loop) in Java to iterate over
collections or arrays?
10. What are the benefits of using control structures like loops and conditional statements
in programming?
11. Explain the concept of a control variable in loop structures. How is it used to control
the loop's behavior?
12. Compare and contrast the while loop and the do-while loop. In what situations would
you use each one?
13. Discuss the concept of infinite loops. How can they occur, and how can they be
prevented?
14. How does the concept of short-circuit evaluation apply to logical operators in control
structures?
15. Explain the difference between a pre-test loop and a post-test loop. Provide examples
of each.
16. How can control structures like if-else statements and loops be used to implement
algorithms?

www.vertexitservices.com Call On: +91 9763 9763 33


17. Discuss the role of the default case in a switch statement. When is it necessary, and
when can it be omitted?
18. Explain the concept of a nested if-else statement. Provide an example of when it might
be used.
19. How do you use the break and continue statements to alter the flow of control in a
loop?
20. Describe the purpose of the ternary operator in Java. How does it differ from an if-
else statement?

www.vertexitservices.com Call On: +91 9763 9763 33


Classes and Objects

1. What is a class in Java, and how is it different from an object?


2. How do you define a class in Java? Provide an example.
3. What is the purpose of a constructor in a Java class? How is it different from other
methods?
4. Explain the concept of instance variables in Java classes. How are they different from
local variables?
5. How do you create an object of a class in Java? Provide an example.
6. Discuss the difference between a class variable and an instance variable in Java.
7. What is the significance of the "new" keyword when creating objects in Java?
8. How do you access instance variables and methods of a class from outside the class?
9. Explain the concept of method overloading in Java classes. Provide an example.
10. How do you use static methods and variables in Java classes? Provide an example.
11. Discuss the concept of inheritance in Java classes. How does it help in code
reusability?
12. What is the difference between inheritance and composition in Java?
13. How do you prevent a class from being instantiated in Java?
14. Explain the purpose of the "this" keyword in Java classes. Provide an example.
15. How do you implement encapsulation in Java classes? Why is it important?
16. What is the difference between an object and a reference in Java?
17. How does Java support the concept of multiple inheritance through interfaces?
18. Explain the concept of method overriding in Java classes. Provide an example.
19. How do you implement composition in Java classes? Provide an example.
20. Discuss the role of access modifiers (public, private, protected) in Java classes.
21. How do you use the "instanceof" operator in Java to check the type of an object?
22. What is the purpose of the "final" keyword in Java classes? How does it affect classes
and methods?
23. How do you implement polymorphism in Java classes? Provide an example.
24. Explain the difference between shallow copy and deep copy of objects in Java.
25. How do you use the "static" keyword in Java classes? Provide examples of static
variables and methods.

www.vertexitservices.com Call On: +91 9763 9763 33


Inheritance

1. What is inheritance in Java, and how does it support the concept of code reuse?
2. How do you define a subclass that inherits from a superclass in Java? Provide an
example.
3. Explain the difference between a superclass and a subclass in Java.
4. How does Java support single inheritance? What are the limitations of single
inheritance?
5. Discuss the concept of method overriding in inheritance. Provide an example.
6. How do you use the "super" keyword to call a superclass constructor or method in
Java?
7. Explain the purpose of the "extends" keyword in Java inheritance.
8. How do you prevent a method from being overridden in a subclass?
9. Discuss the use of the "instanceof" operator in Java inheritance. How is it used to
check the type of an object?
10. What is the difference between inheritance and composition in Java? When would
you use each?
11. How does Java support method overloading in inheritance? Provide an example.
12. Explain the concept of polymorphism in Java inheritance. How does it relate to
method overriding?
13. How do you implement multiple inheritance in Java using interfaces? Provide an
example.
14. Discuss the role of access modifiers (public, private, protected) in inheritance in Java.
15. How do you use the "final" keyword in Java inheritance? What does it mean for
classes and methods?
16. What is the difference between method overloading and method overriding in Java
inheritance? Provide examples.
17. How does Java handle constructor invocation in inheritance? Explain with an
example.
18. Discuss the concept of abstract classes in Java inheritance. How are they different
from concrete classes?
19. How do you use the "abstract" keyword to declare an abstract class or method in
Java?
20. Explain the purpose of the "final" keyword in Java inheritance. How does it affect
classes and methods?
21. How does Java prevent diamond problem in multiple inheritance through interfaces?
22. What is the Object class in Java? How does it relate to inheritance?
23. How can you achieve runtime polymorphism in Java inheritance? Provide an
example.
24. Discuss the use of the "protected" access modifier in Java inheritance. How is it
different from "public" and "private"?
25. How do you use interfaces to achieve multiple inheritance-like behavior in Java?
Provide an example.

www.vertexitservices.com Call On: +91 9763 9763 33


Interface

1. What is an interface in Java, and how does it differ from a class?


2. How do you define an interface in Java? Provide an example.
3. Explain the concept of multiple inheritance through interfaces in Java.
4. Can you instantiate an interface in Java? Why or why not?
5. How do you implement an interface in a class in Java? Provide an example.
6. Discuss the purpose of default methods in interfaces in Java.
7. How do you use static methods in interfaces in Java? Provide an example.
8. Explain the concept of marker interfaces in Java. Provide an example.
9. How do interfaces help achieve loose coupling and high cohesion in Java programs?
10. Discuss the difference between abstract classes and interfaces in Java.
11. Can an interface extend another interface in Java? How does this affect classes
implementing these interfaces?
12. How can you achieve polymorphism using interfaces in Java? Provide an example.
13. Explain the concept of functional interfaces in Java. How are they related to lambda
expressions?
14. How do you use interfaces to define constants in Java? Provide an example.
15. What is the purpose of the "implements" keyword in Java interface implementation?
16. How do interfaces improve the maintainability and flexibility of Java code?
17. Explain the concept of interface segregation principle (ISP) in object-oriented design
and how interfaces help in achieving it.
18. Can an interface have fields? If so, how are they declared and used?
19. How do you use interfaces to achieve dependency injection in Java?
20. Discuss the role of interfaces in Java's collections framework.
21. How can you use interfaces to implement callbacks in Java?
22. Explain the concept of nested interfaces in Java. Provide an example.
23. How do you use interfaces to achieve abstraction and decoupling in Java?
24. Can an interface extend a class in Java? Why or why not?
25. How do you use interfaces to define custom exceptions in Java?

www.vertexitservices.com Call On: +91 9763 9763 33


Exception Handling
1. What is an exception in Java, and why is exception handling important in
programming?
2. How do you handle exceptions in Java? Provide an example using a try-catch block.
3. Explain the purpose of the "throw" keyword in Java exception handling. How is it
different from the "throws" clause?
4. Discuss the difference between checked and unchecked exceptions in Java.
5. How do you create a custom exception class in Java? Provide an example.
6. Explain the role of the finally block in Java exception handling. When is it executed?
7. How can you use multiple catch blocks to handle different types of exceptions in
Java?
8. Discuss the concept of exception propagation in Java. How does it affect the flow of
control in a program?
9. How do you use the try-with-resources statement in Java for automatic resource
management? Provide an example.
10. Explain the purpose of the "throws" clause in Java method declaration. How does it
affect exception handling?
11. How can you handle exceptions in Java without using try-catch blocks? Provide an
alternative approach.
12. Discuss the use of the "assert" statement for handling exceptions in Java. How does
it differ from regular exception handling?
13. How do you use the getMessage() method of the Throwable class to retrieve the
error message associated with an exception?
14. Explain the difference between Error and Exception classes in Java.
15. How do you use the try-catch-finally construct in Java exception handling to ensure
resource cleanup?
16. How do you use the throws keyword in Java to declare exceptions in a method
signature? Provide an example.
17. Explain the difference between the try-catch and try-finally blocks in Java exception
handling.
18. How can you use the try-catch block to catch multiple exceptions in Java 7 and later
versions?
19. Discuss the concept of exception chaining in Java. How can you use the getCause()
method to retrieve the original cause of an exception?
20. How do you use the finally block in Java exception handling to ensure that certain
code is always executed, regardless of whether an exception occurs?
21. Explain the concept of exception handling best practices in Java. What are some
common practices to follow when handling exceptions?
22. How can you use the throws keyword in a method declaration to propagate an
exception to its caller?
23. Discuss the role of the Exception class and its subclasses in Java exception handling.
24. How do you use the try-catch block to handle exceptions thrown by methods in Java
libraries or third-party code?
25. How can you use the try-catch block to handle both checked and unchecked
exceptions in Java?

www.vertexitservices.com Call On: +91 9763 9763 33


Input and Output
1. What is input and output (I/O) in Java, and why is it important in programming?
2. How do you read input from the console in Java? Provide an example using the
Scanner class.
3. Explain the purpose of the System.out.println() method in Java. How is it used for
output?
4. How can you format output in Java using the System.out.printf() method? Provide an
example.
5. Discuss the difference between byte stream and character stream in Java I/O.
6. How do you read and write files in Java using the FileReader and FileWriter classes?
Provide an example.
7. Explain the purpose of the try-with-resources statement in Java I/O. How does it help
manage resources?
8. How can you read and write objects to a file in Java using serialization? Provide an
example.
9. Discuss the concept of buffering in Java I/O. How does it improve I/O performance?
10. How do you use the java.io package to perform file I/O operations in Java?
11. What is the purpose of the java.nio package in Java I/O? How does it differ from the
java.io package?
12. Explain the concept of character encoding in Java I/O. How does it affect text file
operations?
13. How can you handle end-of-file (EOF) in Java when reading input from a file or stream?
14. Discuss the use of the File class in Java for file and directory manipulation.
15. How do you use the PrintWriter class in Java to write formatted text to a file? Provide
an example.
16. How can you read and write binary data in Java using the DataInputStream and
DataOutputStream classes? Provide an example.
17. Explain the concept of character streams in Java I/O. How do they differ from byte
streams?
18. How do you use the BufferedReader and BufferedWriter classes in Java for efficient
text file I/O? Provide an example.
19. Discuss the purpose of the ByteArrayInputStream and ByteArrayOutputStream classes
in Java I/O. How are they used?
20. How can you use the java.nio.file package in Java for file I/O operations? Provide an
example.
21. Explain the concept of file permissions in Java. How can you set and check file
permissions using the java.nio.file.attribute package?
22. How do you use the Scanner class in Java to read input from various sources, such as
files and strings? Provide examples.
23. Discuss the use of the RandomAccessFile class in Java for random access file I/O
operations. Provide an example.
24. How can you use the java.io.PrintWriter class in Java to write formatted text to a file?
Provide an example.
25. Explain the purpose of the ObjectInputStream and ObjectOutputStream classes in Java
I/O. How are they used for object serialization?

www.vertexitservices.com Call On: +91 9763 9763 33

You might also like