wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

JAVA

Total questions: 38

Worksheet time: 19mins

Name
Class
Date
1.

What will be the output of the following Java code?

  1. class increment {

  2. public static void main(String args[])

  3. {

  4. int g = 3;

  5. System.out.print(++g * 8);

  6. }

  7. }

a)

A) 32

b)

B) 33

c)

C) 24

d)

D) 25

2.

What will be the output of the following Java program?

  1. class output {

  2. public static void main(String args[]){

  3. double a, b,c;

  4. a = 3.0/0;

  5. b = 0/4.0;

  6. c=0/0.0; 

  7. System.out.println(a);

  8. System.out.println(b);

  9. System.out.println(c);

  10. }

  11. }

a)
Infinity
b)
NaN
c)
Infinity 0.0 NaN
d)
0.0
3.

What will be the output of the following Java program?

  1. class variable_scope

  2. {

  3. public static void main(String args[])

  4. {

  5. int x;

  6. x = 5;

  7. {

  8. int y = 6;

  9. System.out.print(x + " " + y);

  10. }

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

  12. }

  13. }



a)

a) Compilation error

b)

b) Runtime error

c)

c) 5 6 5 6

d)

d) 5 6 5

4.

What will be the output of the following Java program?

  1. class leftshift_operator

  2. {

  3. public static void main(String args[])

  4. {

  5. byte x = 64;

  6. int i;

  7. byte y;

  8. i = x << 2;

  9. y = (byte) (x << 2);

  10. System.out.print(i + " " + y);

  11. }

  12. }

a)
256 0
b)
256 64
c)
64 0
d)
128 0
5.

What will be the output of the following Java code?

  1. class box

  2. {

  3. int width;

  4. int height;

  5. int length;

  6. }

  7. class main

  8. {

  9. public static void main(String args[])

  10. {

  11. box obj = new box();

  12. obj.width = 10;

  13. obj.height = 2;

  14. obj.length = 10;

  15. int y = obj.width * obj.height * obj.length;

  16. System.out.print(y);

  17. }

  18. }


a)

100

b)

400

c)

200

d)

12

6.

What is Truncation in Java?

a)

Floating point value is assigned to floating type

b)

Floating point value assigned to integer type.

c)

Integer point value assigned to integer type

d)

Integer point value assigned to floating type

7.

What will be the output of the following Java program?

  1. class Output

  2. {

  3. public static void main(String args[])

  4. {

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

  6. for ( int i = 0; i < arr.length - 2; ++i)

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

  8. }

  9. }

a)
1 2 3 4
b)
2 3 4 5
c)
0 1 2
d)
1 2 3
8.

What will be the output of the following Java code snippet?

  1. class abc

  2. {

  3. public static void main(String args[])

  4. {

  5. if(args.length>0)

  6. System.out.println(args.length);

  7. }

  8. }

a)

The snippet compiles and runs but does not print anything

b)
The output will be an error message if no arguments are provided.
c)
The output will be the first command-line argument passed.
d)
The output will always be zero regardless of the arguments.
9.

What is the extension of compiled java classes?

a)
.class
b)
.classfile
c)
.exe
d)
.java
10.

Which exception is thrown when java is out of memory?

a)
HeapSpaceError
b)
OutOfMemoryError
c)
MemoryOverflowError
d)
InsufficientMemoryError
11.

What will be the output of the following Java code?

  1. class String_demo

  2. {

  3. public static void main(String args[])

  4. {

  5. char chars[] = {'a', 'b', 'c'};

  6. String s = new String(chars);

  7. System.out.println(s);

  8. }

  9. }


a)
abcde
b)
ab
c)
abc
d)
a b c
12.

Which of these are selection statements in Java?

a)
for
b)
while
c)
do while
d)
if, else if, else, switch
13.

What will be the output of the following Java program?

  1. class recursion

  2. {

  3. int func (int n)

  4. {

  5. int result;

  6. if (n == 1)

  7. return 1;

  8. result = func (n - 1);

  9. return result;

  10. }

  11. }

  12. class Output

  13. {

  14. public static void main(String args[])

  15. {

  16. recursion obj = new recursion() ;

  17. System.out.print(obj.func(5));

  18. }

  19. }

a)
5
b)
0
c)
-1
d)
1
14.

What will be the output of the following Java code?

  1. class output

  2. {

  3. public static void main(String args[])

  4. {

  5. String c = "Hello i love java";

  6. boolean var;

  7. var = c.startsWith("hello");

  8. System.out.println(var);

  9. }

  10. }

a)
true
b)
Hello i love java
c)
null
d)
false
15.

Which of these keywords is used to define interfaces in Java?

a)
class
b)
enum
c)
interface
d)
struct
16.

What will be the output of the following Java program?

  1. class output

  2. {

  3. public static void main(String args[])

  4. {

  5. StringBuffer s1 = new StringBuffer("Quiz");

  6. StringBuffer s2 = s1.reverse();

  7. System.out.println(s2);

  8. }

  9. }

a)
Quiz
b)
QiuZ
c)
ziuQ
d)
QuizZ
17.

What will be the output of the following Java code?

  1. class Output

  2. {

  3. public static void main(String args[])

  4. {

  5. Integer i = new Integer(257);

  6. byte x = i.byteValue();

  7. System.out.print(x);

  8. }

  9. }

a)
257
b)
0
c)
-1
d)
1
18.

What will be the output of the following Java program?

  1. class Output

  2. {

  3. public static void main(String args[])

  4. {

  5. double x = 2.0;

  6. double y = 3.0;

  7. double z = Math.pow( x, y );

  8. System.out.print(z);

  9. }

  10. }

a)
9.0
b)
8.0
c)
6.0
d)
4.0
19.

Which of the following is a superclass of every class in Java?

a)
Object
b)
String
c)
Integer
d)
List
20.

What will be the output of the following Java code?

  1. class Output

  2. {

  3. public static void main(String args[])

  4. {

  5. double x = 3.14;

  6. int y = (int) Math.ceil(x);

  7. System.out.print(y);

  8. }

  9. }

a)
3
b)
5
c)
2
d)
4
21.

What will be the output of the following Java program?

  1. import java.net.*;

  2. class networking

  3. {

  4. public static void main(String[] args) throws Exception

  5. {

  6. URL obj = new URL("https://www.sanfoundry.com/javamcq");

  7. URLConnection obj1 = obj.openConnection();

  8. int len = obj1.getContentLength();

  9. System.out.print(len);

  10. }

  11. }

a)

126

b)

Runtime error

c)

127

d)

Compilation error

22.

Which of the below is not a Java Profiler?

a)
Java Virtual Machine
b)
Not a Java Profiler
c)
Java Compiler
d)
Java Debugger
23.

What will be the output of the following Java program?

  1. import java.net.*;

  2. class networking

  3. {

  4. public static void main(String[] args) throws MalformedURLException

  5. {

  6. URL obj = new URL("https://www.sanfoundry.com/javamcq");

  7. System.out.print(obj.toExternalForm());

  8. }

  9. }

a)
https://www.example.com
b)
https://www.google.com
c)
https://www.sanfoundry.com/javamcq
d)
http://www.sanfoundry.com/javamcq
24.

What will be the output of the following Java code snippet?

  1. import java.util.*;

  2. class Arraylists

  3. {

  4. public static void main(String args[])

  5. {

  6. ArrayList obj = new ArrayList();

  7. obj.add("A");

  8. obj.add("B");

  9. obj.add("C");

  10. obj.add(1, "D");

  11. System.out.println(obj);

  12. }

  13. }

a)
[C, B, A, D]
b)
[A, D, B, C]
c)
[A, B, C, D]
d)
[D, A, B, C]
25.

Which of these packages contains the error StackOverflowError in Java?

a)
java.lang
b)
java.awt
c)
java.io
d)
java.util
26.

What will be the output of the following Java program?

  1. import java.util.*;

  2. class Collection_iterators

  3. {

  4. public static void main(String args[])

  5. {

  6. LinkedList list = new LinkedList();

  7. list.add(new Integer(2));

  8. list.add(new Integer(8));

  9. list.add(new Integer(5));

  10. list.add(new Integer(1));

  11. Iterator i = list.iterator();

  12. Collections.reverse(list);

  13. Collections.sort(list);

  14. while(i.hasNext())

  15. System.out.print(i.next() + " ");

  16. }

  17. }

a)
1 2 5 8
b)
8 5 2 1
c)
5 1 2 8
d)
2 8 5 1
27.

Which of these keywords are used for the block to be examined for exceptions?

a)
except, handle
b)
throw, finally
c)
assert, validate
d)
try, catch
28.

What will be the output of the following Java code?

  1. class newthread extends Thread

  2. {

  3. Thread t1, t2;

  4. newthread()

  5. {

  6. t1 = new Thread(this,"Thread_1");

  7. t2 = new Thread(this,"Thread_2");

  8. t1.start();

  9. t2.start();

  10. }

  11. public void run()

  12. {

  13. t2.setPriority(Thread.MAX_PRIORITY);

  14. System.out.print(t1.equals(t2));

  15. }

  16. }

  17. class multithreaded_programing

  18. {

  19. public static void main(String args[])

  20. {

  21. new newthread();

  22. }

  23. }

a)
true
b)

falsefalse

c)
Thread_1
d)
Thread_2
29.

Which one of the following is not an access modifier?

a)
private
b)
protected
c)
public
d)

void

30.

What will be the output of the following Java program?

  1. final class A

  2. {

  3. int i;

  4. }

  5. class B extends A

  6. {

  7. int j;

  8. System.out.println(j + " " + i);

  9. }

  10. class inheritance

  11. {

  12. public static void main(String args[])

  13. {

  14. B obj = new B();

  15. obj.display();

  16. }

  17. }

a)
Runtime exception
b)
Output: 0 0
c)
Output: 0 1
d)
Compilation error
31.

What is the numerical range of a char data type in Java?

a)
0 to 255
b)
1 to 1000
c)
0 to 65535
d)
-128 to 127
32.

Which class provides system independent server side implementation?

a)
WebSocket
b)
RESTController
c)

Serversocket

d)
HTTPHandler
33.

What will be the output of the following Java program?

  1. class overload

  2. {

  3. int x;

  4. double y;

  5. void add(int a , int b)

  6. {

  7. x = a + b;

  8. }

  9. void add(double c , double d)

  10. {

  11. y = c + d;

  12. }

  13. overload()

  14. {

  15. this.x = 0;

  16. this.y = 0;

  17. }

  18. }

  19. class Overload_methods

  20. {

  21. public static void main(String args[])

  22. {

  23. overload obj = new overload();

  24. int a = 2;

  25. double b = 3.2;

  26. obj.add(a, a);

  27. obj.add(b, b);

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

  29. }

  30. }

a)
4 6.4
b)
0 0
c)
4 3.2
d)
2 3.2
34.

Which of the following is true about servlets?

a)
Servlets are client-side components that manage user interfaces.
b)
Servlets are only used for database connections in web applications.
c)
Servlets can only handle static content and not dynamic requests.
d)

Servlets execute within the address space of web server, platform independent and uses the functionality of java class libraries

35.

Which of the following contains both date and time?

a)

dateTime

b)
DateOnly
c)
TimestampWithoutDate
d)
TimeOnly
36.

Which of the following is advantage of using JDBC connection pool?

a)
Limited scalability for large applications.
b)
Improved performance through connection reuse.
c)
Higher latency due to frequent connections.
d)
Increased complexity in database management.
37.

Which of the following is advantage of using PreparedStatement in Java?

a)
Increased risk of SQL injection
b)
Slower execution time
c)
More complex code structure
d)

Prevention of SQL injection

38.

Which one of the following contains date information?

a)

java.sql.TimeStamp

b)

java.io.timeStamp

c)

java.io.dateTime

d)

java.io.Time