wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Java Fundamentals

Total questions: 70

Worksheet time: 38mins

Name
Class
Date
1.

Which of the following option leads to the portability and security of Java?

a)

Bytecode is executed by JVM

b)

The applet makes the Java code secure and portable

c)

Use of exception handling

d)

Dynamic binding between objects

2.

Which of the following is not a Java features?

a)

Dynamic

b)

Architecture Neutral

c)

Use of pointers

d)

Object-oriented

3.

_____ is used to find and fix bugs in the Java programs.

a)

JVM

b)

JRE

c)

JDK

d)

JDB

4.

Which of the following is a valid declaration of a char?

a)

char ch = '\utea';

b)

char ca = 'tea';

c)

char cr = \u0223;

d)

char cc = '\itea';

5.

What does the expression float a = 35 / 0 return?

a)

0

b)

Not a number

c)

Infinity

d)

Run time exception

6.

Evaluate the following Java expression, if x=3, y=5, and z=10:

++z + y - y + z + x++

a)

24

b)

23

c)

20

d)

25

7.

Which of the following for loop declaration is not valid?

a)

for ( int i = 99; i >= 0; i / 9 )

b)

for ( int i = 7; i <= 77; i += 7 )

c)

for ( int i = 20; i >= 2; - -i )

d)

for ( int i = 2; i <= 20; i = 2* i )

8.

Which of the following is not OOPS concept in Java?

a)

Inheritance

b)

Encapsulation

c)

Polymorphism

d)

Compilation

9.

Which of the following is a type of polymorphism in Java?

a)

Compile time polymorphism

b)

Execution time polymorphism

c)

Multiple polymorphism

d)

Multilevel polymorphism

10.

When does method overloading is determined?

a)

At run time

b)

At compile time

c)

At coding time

d)

At execution time

11.

Which concept of Java is a way of converting real world objects in terms of class?

a)

Polymorphism

b)

Encapsulation

c)

Abstraction

d)

Inheritance

12.

Which concept of Java is achieved by combining methods and attribute into a class?

a)

Encapsulation

b)

Inheritance

c)

Polymorphism

d)

Abstraction

13.

Which component is used to compile, debug and execute java program?

a)

JVM

b)

JDK

c)

JIT

d)

JRE

14.

Which of the below is invalid identifier with the main method?

a)

public

b)

static

c)

final

d)

private

15.

Which of the below is invalid identifier with the main method?

a)

public

b)

static

c)

final

d)

private

16.

What is use of interpreter?

a)

They convert bytecode to machine language code

b)

They read high level code and execute them

c)

They are intermediated between JIT and JVM

d)

It is a synonym for JIT

17.

What is the extension of compiled java classes?

a)

.java

b)

.class

c)

.txt

d)

.js

18.

Which of these keywords is used to make a class?

a)

class

b)

struct

c)

int

d)

none of the mentioned

19.

Which of these statement is incorrect?

a)

Every class must contain a main() method

b)

Applets do not require a main() method at all

c)

There can be only one main() method in a program

d)

main() method must be made public

20.

Which of the following statements is correct?

a)

Public method is accessible to all other classes in the hierarchy

b)

Public method is accessible only to subclasses of its parent class

c)

Public method can only be called by object of its class

d)

Public method can be accessed by calling object of the public class

21.

Which of this keyword must be used to inherit a class?

a)

super

b)

this

c)

extend

d)

extends

22.

Which of the following is a valid declaration of a char?

a)

char ch = '\utea';

b)

char ca = 'tea';

c)

char cr = \u0223;

d)

char cc = '\itea';

23.

To print "Hello, world" on the monitor, use the following Java statement:

a)

System.out.println("Hello, world");

b)

System Print = "Hello, world";

c)

SystemOutPrintln('Hello, world');

d)

system.out.println{Hello, world};

24.

Choose the correct order of the Java program execution.

A. Class Loader

B. Interpretation

C. Compilation

D. Byte Code Verification

E. Java Source Code

F. Execution

a)

E-C-A-D-B-F

b)

A-C-B-D-F-E

c)

E-C-B-A-D-F

d)

E-B-C-A-D-F

25.

Which of the following is used to load a .class file?

a)

Class Loader

b)

JIT compiler

c)

Byte Code verifier

d)

Interpreter

26.

How many class files will be created for the following code?

class A { .... }

class B { .... }

public class C

{

public static void main(String args[])

{

...... } }

Note: Assume that A,B and C are in same file TestClass.java

a)

1

b)

2

c)

3

d)

0

27.

What will be the Output o the following code?

class Sample{

public static void main(String args[])

{

int len = args.length;

System.out.println(len);

}

}

a)

Compilation Error

b)

Runtime Error

c)

The program compiles and executes successfully but prints nothing

d)

The program compiles and executes successfully and prints 0

28.

Which method to use to find length of a string?

a)

length()

b)

size()

c)

long()

d)

count()

29.

Which data type gets returned from length() method in String class?

a)

double

b)

String

c)

int

d)

number

30.

String txt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


What is the correct way to find the length of "txt" string?

a)

int len = length(txt);

b)

float len = txt.length();

c)

int len = txt.length();

d)

double len = length(txt);

31.

Choose correct purpose of indexOf() in String class.

a)

returns the index (the position) of the last occurrence of a specified text in a string

b)

returns the character of the first occurrence of a specified character in a string

c)

returns the index (the position) of the first occurrence of a specified text in a string (excluding whitespace)

d)

returns the index (the position) of the first occurrence of a specified text in a string (including whitespace)

32.

String x = "10";

String y = "20";

String z = x + y;

System.out.println(z);

Guess the output.

a)

1020

b)

10 20

c)

30

d)

"10 20"

33.

Choose most appropriate purpose of trim() method in String?

a)

to remove white spaces

b)

to get a substring of the string

c)

to cut String at desired index

d)

to remove extra white spaces from start and end of String

34.

What is the most appropriate way to check if two Strings are identical?

a)

string1 == string2

b)

string1.equals(string2)

c)

string1.same(string2)

d)

string1.equalsIgnorecase(string2)

35.

int[] myArray = {11, 22, 33, 44, 55};

which code is correct to print length of above int-array?

a)

System.out.println(int[].myArray.length());

b)

System.out.println(myArray.length);

c)

System.out.println(myArray.length());

d)

System.out.println(int[].myArray.size());

36.

String[] friends = {"John", "Mike", "Samantha", "Monica"};

which code is correct to print "Samantha" using array functionality?

a)

System.out.println(friends[2]);

b)

System.out.println(friends[3]);

c)

System.out.println(friends["Samantha"]);

d)

System.out.println(friends{2});

37.

Java code to create an array of student id with element 115331, 450055, 555641, 875560 and 987555.

a)

int [ ] id = new {115331, 450055, 555641, 875560, 987555};

b)

int [5] id = {115331, 450055, 555641, 875560, 987555};

c)

int [ ] id = {115331, 450055, 555641, 875560, 987555};

d)

int [ ] id = new int [5];

38.

Java code to create a non-element array of student name with size 15.

a)

String [ ] name = new String;

b)

String [ ] name = new String [15];

c)

name = new String [15];

d)

String [15] name = new String [ ];

39.

What is the purpose of 'throw' keyword

a)

To Raise an exception explicityly

b)

To Raise an exception implicityly

c)

To create user defined exceptions

d)

To create custom exceptions

40.

What is an exception

a)

Error occurred during the execution of a program

b)

Bug occurred during the compile time of a program

c)

Simply an Error

d)

It is related to input values

41.

Exception classes belongs to following package

a)

import java.io.*

b)

import java.lang.*

c)

import java.util.*

d)

import java.lang.Exception.*

42.

Which of these keywords is not a part of exception handling?

a)

try

b)

finally

c)

thrown

d)

catch

43.

Choose the CORRECT exception


String department="JTMK";

Integer no=Integer.parseInt(department);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundsException

44.

Choose an exception if attempt to divide number by zero


int number = 89 / 0;

System.out.println("The answer is " + number);

a)

ArithmeticException

b)

NullPointerException

c)

NumberFormatException

d)

ArrayIndexOutOfBoundException

45.

Which line or lines should we get inside a try block to avoid errors?

a)

3

b)

4

c)

6

d)

2

46.

Choose the CORRECT mechanism of exception handling.

a)

try block --> catch block --> finally block

b)

try block --> catch block --> throws exception

c)

try block --> catch block --> finally --> throws exception

d)

try block --> throws exception--> catch block --> finally block

47.

Identify the CORRECT keyword(s) allow the exception to be caught by the catch block.

**You may choose more than 1 answer

a)

try

b)

throw

c)

catch

d)

finally

48.

Identify in which block should handling statements are written for any exception caught?

a)

try

b)

catch

c)

Throws

d)

throws

49.

What are immediate child classes of Throwable

a)

Error

b)

Exception

c)

CheckedException

d)

UnCheckedException

50.

class IT

{

static public void main(String...args)

{

String s=null;

System.out.println("length is:"+s.length());

}

}

a)

It throws NullPointerException

b)

it throws IOException

c)

It throws StringExeption

d)

output is 0

51.

public class Foo

{

public static void main(String[] args)

{

try

{

return;

}

finally

{

System.out.println( "Finally" );

}

}

}

a)

Finally

b)

Compilation fails.

c)

The code runs with no output

d)

An exception is thrown at runtime.

52.

public class X

{

public static void main(String [] args)

{

try

{

badMethod();

System.out.print("A");

}

catch (Exception ex)

{

System.out.print("B");

}

finally

{

System.out.print("C");

}

System.out.print("D");

}

public static void badMethod()

{

throw new Error(); /* Line 22 */

}

}

a)

ABCD

b)

Compilation fails.

c)

C is printed before exiting with an error message.

d)

BC is printed before exiting with an error message

53.

Exception and Error are direct subclasses of?

a)

.BaseException

b)

Throwable

c)

Object

d)

RuntimeException

54.

What block is always executed, independently of a exception being raised?

a)

throws

b)

finally

c)

catch

d)

throw

55.

Which of these class is highest in hierarchy in java

a)

java.lang.Exception

b)

java.lang.Error

c)

java.lang.Throwable

d)

java.lang.Object

56.

Exception is a class/interface/abstract class/other?

a)

Class

b)

Interface

c)

Abstract class

d)

Other

57.

The code within the ----------- block will always be executed whether or not an exception is thrown.

a)

try

b)

catch

c)

finally

d)

throw

58.

The code within the ----------- block will always be executed whether or not an exception is thrown.

a)

try

b)

catch

c)

finally

d)

throw

59.

class exception_handling

{ public static void main(String args[])

{ try

{ int a, b;

b = 0;

a = 5 / b;

System.out.print("A");

} catch(ArithmeticException e)

{ System.out.print("B");

}

finally

{ System.out.print("C");

}

}

}

a)

A

b)

B

c)

AC

d)

BC

60.

public class X

{

public static void main(String [] args)

{

try

{

badMethod();

System.out.print("A");

}

catch (RuntimeException ex) /* Line 10 */

{

System.out.print("B");

}

catch (Exception ex1)

{

System.out.print("C");

}

finally

{

System.out.print("D");

}

System.out.print("E");

}

public static void badMethod()

{

throw new RuntimeException();

}

}

a)

BD

b)

BDE

c)

BD

d)

DE

61.

class Main {

public static void main(String args[]) {

try {

throw 10;

}

catch(int e) {

System.out.println("Got the Exception " + e);

}

}

}

a)

Got the Exception 10

b)

Got the Exception 0

c)

Compiler Error

d)

none

62.

Read the following code

a)

The keywords public and abstract cannot be used together.

b)

The method numberOfStudent() in class School must have a body.

c)

You must add a return statement in method numberOfStudent().

d)

Class School must be defined abstract.

63.

Which of the following is the legal abstract class?

a)

class A { abstract void unfinished() { } }

b)

class A { abstract void unfinished(); }

c)

abstract class A { abstract void unfinished(); }

d)

public class abstract A { abstract void unfinished(); }

64.

What will happen after compiling this program?

a)

Successful run and print 37

b)

Compilation error due to line 1

c)

Compilation error due to line 2

d)

Runtime error

e)

None of these

65.

What happen in case of multiple catch blocks

a)

Either super or subclass can be caught first.

b)

The superclass exception must be caught first.

c)

The superclass exception cannot caught first.

d)

None of these

66.

What will be the output?

a)

inCatchBlock

b)

inCatchBlock inFinallyBlock

c)

inFinallyBlock

d)

The program will return without printing anything

67.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

68.

What will this print?

a)

There was an error

b)

1

c)

9

d)

0

69.

What will this program print?

a)

Nothing, there is an error

b)

There was a ValueError, please try again

c)

num1num2num1^{num2}

d)

num2num1num2^{num1}

70.

OOPS concepts are

a)

Abstraction Encapsulation Polymorphism

b)

Class Object Abstraction Encapsulation Polymorphism Inheritance

c)

Polymorphism

d)

Inheritance