wayground logo

Free Printable Worksheets

Font size

S
M
L
XL
Worksheets

Fundamentalls of java

Total questions: 122

Worksheet time: 1hrs 19mins

Name
Class
Date
1.

what is size of int in Java?

a)

2 bytes

b)

4 bytes

c)

6 bytes

d)

8 bytes

2.

what is size of long int in Java?

a)

2 bytes

b)

4 bytes

c)

6 bytes

d)

8 bytes

3.

what is size of char in Java?

a)

2 bytes

b)

4 bytes

c)

6 bytes

d)

1 bytes

4.

String in a Java is primitive (built-in) data type or class?

a)

data type

b)

class

5.

What size will be required to store value 324.98 in Java?

a)

2

b)

3

c)

6

d)

8

6.

Which of these can not be used for a variable name in Java and any other programming languages?

a)

identifier

b)

keyword

c)

identifier & keyword

d)

none of the mentioned

7.

Import java.lang.*;

class Rectangle

{

int l,b,a;

a=l*b;

void fun()

{

l=10;

b=20;

system.out.println("Area = ", a);

}

}

Identify errors in above program segment.

a)

Error in import line and Error in println line both

b)

Error in import line

c)

Error in println line

d)

No error at all

8.

Which of the following rule is INCORRECT in Java?

a)

Class should not be end with semi-colon

b)

Constants to be written in CAPITAL letters

c)

main() should be inside the class

d)

We can write Java program without using class

9.

What is the output of the following?

int x = 9;

if (x>9)

System.out.println("Hello");

else

System.out.println("World");

a)

Hello

b)

World

c)

Syntax Error

d)

Compilation Error

10.

What is the output of the following:

for(int x=1 ; x<5 ; x++){

System.out.print(x);

}

a)

1234

b)

01234

c)

012345

d)

12345

11.

What is the output of the following:

String myWord = "FUN";

String anotherWord = myWord.toLowerCase();

System.out.println(myWord);

a)

fun

b)

Fun

c)

FUN

d)

"fun"

12.

What is the output of the following:

int y = 5;

do{

System.out.println("This is not the output!");

y++;

}while(y>5);

a)

6

b)

Compilation error

c)

This is not the output!

(printed 5 times)

d)

This is not the output!

13.

int x = 25;

if(5*5)

System.out.println("The answer is 25!");

else

Sytem.out.println("The answer is zero");

a)

true

b)

The answer is 25!

c)

The answer is zero

d)

Compilation Error

14.

What is the output of the following?

boolean yes=true, no=false;

if (!yes||no)

System.out.println("maybe");

else

System.out.println("yes!");

a)

no

b)

maybe

c)

Compilation Error

d)

yes!

15.

What is the output of the following?

String a = "I will pass the exams.";

String b = " I will pass the exams. ";

if (a.equals(b))

System.out.println("fail");

else

System.out.println(b.substring(9,12));

a)

pass

b)

fail

c)

Compilation Error

d)

I will pass the exams.

16.

What is the output of the following?

String a = "Are you ready kids?";

String b = "Aye! Aye! Captain!";

System.out.println(a.concat(b));

a)

Are you ready kids? Aye! Aye! Captain!

b)

Compilation Error

c)

Are you ready kids?Aye! Aye! Captain!

d)

Aye! Aye! Captain!

17.

What is the output of the following code?

String a = "19";

String b = "75";

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

a)

19 75

b)

94

c)

Compilation Error

d)

1975

18.

What method of class String is used to remove white spaces at the beginning and end of String objects?

a)

remove()

b)

trim()

c)

concat()

d)

Substring()

19.
What is an error in a program that prevents the program from running as expected?
a)
Algorithm
b)
Program
c)
Bug
d)
Code
20.
What is finding and fixing errors in programs?
a)
Debugging
b)
Program
c)
Bug
d)
Code
21.

In Java, lists and arrays are the same thing

a)

True

b)

False

22.

If data members are private, what can we do to access them from the class object?

a)

Create public member functions to access those data members

b)

Create private member functions to access those data members

c)

Create protected member functions to access those data members

d)

Private data members can never be accessed from outside the class

23.

Which feature can be implemented using encapsulation?

a)

Inheritance

b)

Abstraction

c)

Polymorphism

d)

Overloading

24.

Which inheritance in java programming is not supported

a)

Multiple inheritance

b)

hierarchal inheritance

c)

Multilevel inheritance

d)

Single inheritance

25.

What is subclass in java?

a)

A subclass is a class that extends another class

b)

A subclass is a class declared inside a class

c)

Both above.

d)

None of the above.

26.

If class B is subclassed from class A then which is the correct syntax

a)

class B:A{}

b)

class B extends A{}

c)

class B extends class A{}

d)

class B implements A{}

27.

Find which of the following uses encapsulation?

a)

void main(){

int a;

void fun() { int a=10;

System.out.println(a);

fun(); }

b)

class student{

int a;

public int b; };

c)

class student {

int a;

public void disp(){

System.out.println(a);

} }

d)

static topper() {

char name[10];

public int marks; }

28.

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

a)

super

b)

this

c)

extent

d)

extends

29.

Java is a (a)   language

30.

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

31.

The keyword "method" is an another name for?

a)

function

b)

variable

c)

class

d)

object

32.

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

33.

What will be the output of the following Java program?

class Output {

public static void main(String args[]) {

int a = 1;

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)

244

d)

234

e)

344

34.

What will be the output of the following Java code?

class operators {

public static void main(String args[]) {

int x = 8;

System.out.println(++x * 3 + " " + x); }

a)

24 8

b)

27 9

c)

24 9

d)

27 8

35.

What will be the output of the following Java code?

class Relational_operator {

public static void main(String args[]) {

int var1 = 5;

int var2 = 6;

System.out.print(var1 > var2); }

}

a)

5

b)

6

c)

FALSE

d)

TRUE

36.

What will be the output of the following Java program?

class booloperators {

public static void main(String args[]) {

boolean var1 = true;

boolean var2 = false;

System.out.println((var1 & var2));

}

}

a)

True

b)

False

c)

1

d)

0

37.

What will be the output of the following Java program?

class squareroot {

public static void main(String args[]) {

double a, b;

a = 3.0;

b = 4.0;

double c = Math.sqrt(a * a + b * b);

System.out.println(c); } }

a)

compilation error

b)

25

c)

7

d)

5

38.

What is the result of the following statement

public static void main(String args[])

{

int var1 = 5;

int var2 = 6;

if ((var2 = 1) == var1)

System.out.print(var2);

else

System.out.print(++var2);

} }

a)

3

b)

5

c)

6

d)

2

39.

Which of the following is/ are feature(s) of OOP?

a)

Classes and Objects

b)

Data abstraction

c)

Data Encapsulation

d)

Inheritance and Polumorphism

40.

Writing a method with the same name but the different signature is called as?

a)

Method overriding

b)

Method overloading

c)

Method chaining

d)

Method encapsulation

41.

this keyword (self in python) is used to refer?

a)

Calling method

b)

Calling data member

c)

Calling object

d)

None of these

42.

Creating a new class with some other class is called as?

a)

Inheritance

b)

Polymorphism

c)

Binding

d)

Encapsulation

43.

Which of the following is not a valid Java collection

a)

HashMap

b)

Vector

c)

ArrayList

d)

LinkedListHashMap

44.

To use foreach loop on a particular collection, collection should implement?

a)

serializable

b)

iterable

c)

closeable

d)

enumerable

45.

Pick the correct way(s) of declaring an array in Java?

a)

int[] x = new int[5]

b)

int[] x = {1,2,3,4}

c)

int[] x = new int[]{1,2,3,4}

d)

int x[5] = {1,2,3,4,5}

46.
We are use Scanner Library in java for...
a)
Give permissions to the user
b)
Correcting software errors
c)
Allows user to read data
d)
Allows the user to enter data
47.

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

48.

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

a)

JVM

b)

JRE

c)

JDK

d)

JDB

49.

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

50.

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

a)

0

b)

Not a number

c)

Infinity

d)

Run time exception

51.

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 )

52.

When does method overloading is determined?

a)

At run time

b)

At compile time

c)

At coding time

d)

At execution time

53.

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

a)

super

b)

this

c)

extend

d)

extends

54.

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

55.

Operator used to access the members of a class /package

a)

(.) Dot

b)

(,) Comma

56.

What is the list nums if it is initially [5, 3, 1] and the following code is executed?

nums.add(6);

nums.add(0, 4);

nums.remove(1);

a)

[4, 3, 1, 6]

b)

[5, 3, 1, 6]

c)

[4, 3, 6]

d)

[4, 5, 3, 6]

57.

What index value is used to locate the last element in the nums ArrayList?

a)

nums.size()-1

b)

nums.length()-1

c)

nums.size()

d)

nums.length

58.

Which statement below is the correct way to retrieve the first element in the nums ArrayList?

a)

nums.get(0)

b)

nums[0]

c)

nums(0)

d)

nums[1]

59.

Given the ArrayList nums with the values [3, 7, 6, 0], what code below is the proper way to change the 7 to be a 5?

a)

nums.set(1, 5)

b)

nums.set(7, 5)

c)

nums.set(5, 2)

d)

nums[2] = 5

60.

What will print when the following code executes?

ArrayList<Integer> list = new ArrayList<Integer>();

list.add(7);

list.add(8);

list.add(9);

System.out.println(list.remove(0));

a)

7

b)

8

c)

9

d)

0

61.

What is printed as a result of executing the following code?

ArrayList<Integer> alist = new ArrayList<Integer>();

alist.add(1);

alist.add(2);

alist.remove(1);

alist.add(1, 3);

alist.set(1, 4);

alist.add(5);

System.out.println(alist);

a)

[1, 4, 5]

b)

[1, 4, 3, 5]

c)

[2, 4, 5]

d)

[2, 4, 3, 5]

62.

What is printed as a result of executing the following code segment?

ArrayList<Double> vals = new ArrayList<Double>();

vals.add(3.5);

vals.add(4.5);

vals.add(5.5);

vals.add( vals.set(0, 2.5) );

System.out.println(vals);

a)

[2.5, 4.5, 5.5, 3.5]

b)

[2.5, 3.5, 4.5, 5.5]

c)

[3.5, 4.5, 5.5, 2.5]

d)

[2.5, 4.5, 5.5]

63.
_________________ is a collection of elements used to store the same type of data.
a)
Array
b)
Switch
c)
Case
d)
Loop
64.
What are the legal indexes for the array ar, given the following declaration:
int[] ar = {2, 4, 6, 8 }
a)
 0, 1, 2, 3
b)
 1, 2, 3, 4
c)
2, 4, 6, 8
d)
 0, 2, 4, 6
65.

For the array:

int stats[3];

What is the range of the index?

a)

0 to 3

b)

0 to 2

c)

1 to 3

d)

0 to 4

66.

int [] nums = {2, 3, 5, 8, 9, 11};

How would you access the fourth element in nums?

a)

nums[4]

b)

nums[3]

c)

nums(4)

d)

nums(3)

67.
Which of the following statements outputs the fifth value in the quizzes array?
a)
System.out.println(quizzes[5]);
b)
quizzes[4];
c)
quizzes[5];
d)
System.out.println(quizzes[4]);
68.
Create an array of 12 strings called months.
a)
String[] months = new String[12];
b)
String months = new String[12];
c)
String[] months = new String[];
d)
String[12] months = new String[];
69.
Given the following method, what would test return if
a = {0, 2, 3, 4} and v = 1?
a)
-1
b)
0
c)
1
d)
2
70.
What value is at index 1 in this array?  String[] names = {"Mack", "Dennis", "Dee", "Charlie"};
a)
Mack
b)
Dennis
c)
Dee
d)
Charlie
71.
What is the correct way to create an array with these three numbers: 12  8  15
a)
int[] = {12, 8, 15};
b)
int[] nums = {12  8  15};
c)
int[] nums = {12, 8, 15}
d)
int[] nums = {12, 8, 15};
72.

List all parameters that exist in this program segment:-

a)

double point, String student

b)

point, student

c)

name, mark

d)

name[i], mark[i]

73.

Identify the error that exist in this program segment:-

a)

An array data type cannot be use as argument.

b)

The argument was not in the right sequence with the parameter.

c)

The variable name in parameter not as same with the argument.

d)

Program consist 0 error.

74.

Identify the object that used to call a method.

a)

Status

b)

name[i], mark[i]

c)

mtd

d)

void

75.

Fix the error by replacing with the correct java statement.

a)

void Status(String student, double point)

b)

void Status ( name[i], mark[i] )

c)

int Status(String student, double point)

d)

void Status ( String name[i], double mark[i] )

76.

Create a method name Bonus that will read a basic payment and an hour variable as parameter to calculate an extra salary.

a)
b)
c)
d)
77.

Identify the error that exist in this method program:-

a)

Wrong use of return method type

b)

variable extra was unknown.

c)

doble is not one of the data type

d)

A wrong use of hour data type.

78.

Identify the error that exist in this method program:-

a)

Wrong use of return method type

b)

variable extra was unknown.

c)

doble is not one of the data type

d)

A wrong use of hour data type.

79.

Identify the error that exist in this method program:-

a)

Wrong use of return method type

b)

variable extra was unknown.

c)

All of the parameters data type was not stated.

d)

missing { } in if statement.

80.

A variable that holds words

a)

String

b)

boolean

c)

int

d)

float

e)

A dictionary

81.

A variable that holds true or false

a)

String

b)

boolean

c)

int

d)

float

e)

Alex Trebek

82.

-Contains no duplicate elements.

-Can contain at most one Null value.

-Elements are not key-value pairs.

-Accessing an element can be almost as fast as performing a similar operation on an array.


Which of these classes provides the specified features?

a)

LinkedList

b)

TreeMap

c)

HashSet

d)

LinkedHashMap

e)

HashMap

83.

Which of the following statement about Set is true?

a)

A HashSet cannot have a null value.

b)

Set allows duplicate values.

c)

Set allows null values but only single occurrence.

d)

Set extends the Java.util.collection interface.

84.

-Entries are organized as key/value pairs.

-Duplicate entries replace old entries.

-Entries are sorted using a Comparator or the Comparable interface.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

SortedSet

e)

SortedMap

85.
a)

{1=C, 2=JavaEE, 4=Python, 3=PHP}

b)

{1=C, 2=JavaSE, 3=Python, 4=PHP}

c)

{1=C, 2=JavaEE, 3=Python, 4=PHP}

d)

{1=C, 2=JavaSE,2=JavaEE, 3=Python, 4=PHP}

e)

NullPointerException

86.
a)

{null=null, a=null, b=JavaSE, c=PHP, d=Python}

b)

{a=null, b=JavaSE, c=PHP, d=Python}

c)

{b=JavaSE, c=PHP, d=Python}

d)

Compilation Fails

87.
a)

4

b)

5

c)

6

d)

12

88.

-Entries are not organized as key/value pairs.

-Duplicate entries are rejected.

Which interface of the java.util package offers the specified behavior?

a)

List

b)

Map

c)

Set

d)

None of the above

89.
a)

[Computer, Network, Networks, Programming, Security]

b)

[Computer, Programming, Network, Networks, Programming, Security]

c)

[Computer, Programming, Computer, Networks, Network, Security]

d)

[Computer, Networks, Network, Programming, Security]

90.

Which of the following is/are correct statements?

a)

HashSet class implements the Set interface

b)

HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet.

c)

HashSet permits to have a single null value.

d)

HashMap does not allow duplicate keys however it allows to have duplicate values.

e)

HashMap and HashSet are threadsafe

91.

Which of the following statement(s) is/are TRUE?

a)

Both HashMap and HashSet are not synchronized.

b)

HashMap does not allow duplicate keys however it allows to have duplicate values.

c)

HashMap permits single null key and any number of null values.

d)

Both HashMap and HashSet are synchronized.

92.
a)

{"Computer", "Programming","Networks","null"}

b)

{"Computer", "Programming","Networks",null,"null"}

c)

Line 11 : Genetates NullPointer Exception

d)

Line12: Generates NullPointerException

93.

Which of the following statement(s) is/are FALSE?

a)

TreeSet does not preserve the insertion order of elements but elements are sorted by keys.

b)

Objects in a TreeSet are stored in a sorted and ascending order.

c)

TreeSet does not allow to insert Heterogeneous objects. It will throw classCastException at Runtime if trying to add hetrogeneous objects.

d)

Comparator is note provided for TreeSet.

94.

Which of the following is/are FALSE about HashMap in java?

a)

It provides the basic implementation of Map interface of Java.

b)

HashMap doesn’t allow duplicate keys but allows duplicate values.

c)

HashMap allows null key also but only once and multiple null values.

d)

Unsynchronized.

e)

Synchronized

95.

Which of the following is/ are True ?

a)

Set is a group of ordered objects without duplicates

b)

Map is essentially a set of (key,value) pairs with unique keys, supporting a lookup operation to find the value associated with a given key

c)

List is a sequence of objects, with a distinguished first object, and in which each object has a unique successor.

d)

Bag is a group of unordered objects allowing duplicates

96.

Which statement(s) are false about maps in java?

a)

A Map cannot contain duplicate keys and each key can map to at most one value.

b)

A Map is an object that maps keys to values, or is a collection of attribute-value pairs.

c)

A Map cannot contain duplicate keys and each key can map to at more than one value.

d)

A Map is an object that maps functions to values, or is a collection of attribute-value pairs.

97.

Which statement(s) are false about maps in java?

a)

A Map cannot contain duplicate keys and each key can map to at most one value.

b)

A Map is an object that maps keys to values, or is a collection of attribute-value pairs.

c)

A Map cannot contain duplicate keys and each key can map to at more than one value.

d)

A Map is an object that maps functions to values, or is a collection of attribute-value pairs.

98.

List listNames = new ArrayList();

listNames.add("Tom");

listNames.add("Mary");

listNames.add("Peter");


String name2 = (String) listNames.get(1);

System.out.println(name2);

What is the output of the above code

a)

Tom

b)

Mary

c)

Peter

d)

raises Exception

99.

Which of the following statement is not True about Generics in java?

a)

Generics are features of the Java programming language that allow programmers to write parameterized code.

b)

With generics, the compiler can not detect violations at compile time, thus eliminating potential bugs.

c)

Due to the demand of safer code, generics were added to the Java programming language.

d)

generics allow you to write generic code

e)

Stronger type checks at compile time

100.

When do we use sets in java program?

a)

To allow null and duplicate elements

b)

You want to store elements distinctly without duplication, or unique elements.

c)

to allow t contain duplicate keys and each key can map to at most one value

d)

To perform lookups by keys

101.

Describe method.

a)

A component of a program in a big program.

b)

A program to perform a specific task.

c)

A component of a program written to perform a specific task.

d)

A variable that use to read an input.

102.

This is a general structure a method.

a)

<Returnvalue type> <Method Name> (argument / s)

b)

<Modifier> < type> <Method Name> (parameter / s)

c)

public static void main (String[ ] args)

d)

<Method Name> (parameter)

103.

Fix the syntax error in Line 8.

a)

Cylinder sc = new System.in( );

b)

Scanner sc = new Scanner (System.in);

c)

Cylinder Volume = new Volume( );

d)

Cylinder call = new Cylinder( );

104.
USE THIS METHOD IF YOU WANT TO DISPLAY UNFORMATTED CONTENT THAT INCLUDES A LINE TERMINATION AT THE END OF THE OUTPUT.
a)
System.out.println()
b)
System.out.printf()
c)
System.out.print()
d)
String.format()
105.
WHICH TYPE OF STATEMENT IS THE FOLLOWING:
double quiz1 = 0.0;
a)
Variable Declaration
b)
Variable Assignment
c)
Variable Initialization
d)
None of the above
106.
WHAT TYPE OF CLASS OBJECT NEEDS TO BE CREATED IN ORDER TO USE METHODS LIKE nextLine(), nextInt() and nextDouble()?
a)
String
b)
Scanner
c)
System
d)
None of the above
107.

When programming, you have to use Data Types. Which of the following refers to interfaces, classes, and arrays?

a)

Primitive

b)

Non-Primitive

c)

Arrays

d)

Boolean

108.

Which one is a Java valid variable name?

a)

Sum Grades

b)

static

c)

public

d)

FindAverage

109.

The output of the following code is:

int[] array = {2,4,6,8,10};

for (int i=0; i < array.length;i++){

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

}

a)

2

4

6

8

10

b)

2,4,6,8,10

c)

6

6

6

6

6

d)

Nothing

110.

3 + 6 / 2 * 3 - 1 + 2 =

a)

14.5

b)

13

c)

5

111.

Which of the following pairs of declarations will cause an error message?

I.  double x = 14.7;      int y = x;

II.  double x = 14.7;       int y = (int) x;

III.  int x = 14;        double y = x;

a)

None

b)

I only

c)

II only

d)

III only

e)

I and III only

112.

Refer to the following code fragment:


double answer = 13 / 5;

System.out.println("13 / 5 = " + answer);


The output is:

13 / 5 = 2.0


The programmer intends the output to be:

13 / 5 = 2.6


Which of the following replacements for the first line of code will NOT fix the problem?

a)

double answer = (double) 13 / 5;

b)

double answer = 13 / (double) 5;

c)

double answer = 13.0 / 5;

d)

double answer = 13 / 5.0;

e)

double answer = (double) (13 / 5);

113.

Which of the following will evaluate to true only if boolean expressions

A, B, and C are all false?

a)

!A && !(B && !C)

b)

!A || !B || !C

c)

!(A || B || C)

d)

!(A && B && C)

e)

!A || !(B || !C)

114.

Name the data type: '3'

a)

int

b)

char

c)

String

d)

None of the above

115.

How would you declare a variable storing a person's name?

a)

string name = "Jeff";

b)

name String = "Jeff";

c)

String name = Jeff;

d)

String name = "Jeff";

116.

What prints out "I love Java" successfully?

a)

System.out.println(I love Java);

b)

Systemoutprintln("I love Java);

c)

System.out.println("I love" + " Java");

d)

System.out.println("I love Java")

117.

Which will legally declare, construct, and initialize an array?

a)

int [] myList = {"1", "2", "3"};

b)

int [] myList = (5, 8, 2);

c)

int myList [] [] = {4,9,7,0};

d)

int myList [] = {4, 3, 7};

118.

What will be the output of the program?

a)

true

b)

false

c)

Compilation fails

d)

An exception is thrown at runtime

119.

What will be the output of the program?

a)

5 3

b)

6 3

c)

6 4

d)

5 2

120.

What will be the output of the program?

a)

5 3

b)

8 3

c)

8 2

d)

8 5

121.

What will be the output of the program?

a)

0

b)

7

c)

8

d)

14

122.

What will be the output of the program?

a)

ok

b)

ok dokey

c)

dokey

d)

No output is produced