wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

Java Fundamentals

Total questions: 78

Worksheet time: 1hrs 15mins

Name
Class
Date
1.

Command to compile the Java program

a)

java

b)

javac

c)

javap

d)

javadoc

2.

command to run the java program

a)

javac

b)

javap

c)

javadoc

d)

java

3.

Which of this method is given parameter via command line arguments?

a)

main()

b)

recursive() method

c)

Any method

d)

System defined methods

4.

Which of these data types is used to store command line arguments?

a)

Array

b)

Stack

c)

String

d)

Integer

5.

How many arguments can be passed to main()?

a)

Infinite

b)

Only 1

c)

System Dependent

d)

None of the mentioned

6.

Which of these is a correct statement about args in the following line of code?


public static void main(String args[])

a)

args is a String

b)

args is a Character

c)

args is an array of String

d)

args in an array of Character

7.

Can command line arguments be converted into int automatically if required?

a)

Yes

b)

No

c)

Compiler Dependent

d)

Only ASCII characters can be converted

8.

What will be the output of the following Java program, Command line execution is done as – “java Output This is a command Line”?


class Output

{

public static void main(String args[])

{

System.out.print("args[0]");

}

}

a)

java

b)

Output

c)

is

d)

This

9.

What will be the output of the following Java program, Command line execution is done as – “java Output This is a command Line”?


class Output

{

public static void main(String args[])

{

System.out.print("args[3]");

}

}

a)

a

b)

command

c)

is

d)

This

10.

What will be the output of the following Java program, Command line execution is done as – “java Output command Line 10 A b 4 N”?

a)

java

b)

10

c)

20

d)

b

11.

What will be the output of the following Java snippet, if attempted to compile and run this code with command line argument “java abc Rakesh Sharma”?

a)

Compile time error

b)

Compilation but runtime error

c)

Compilation and output Rakesh :-Please pay Rs.2000

d)

Compilation and output Sharma :-Please pay Rs.2000

12.

What will be the output of the following Java snippet, if attempted to compile and execute?

a)

The snippet compiles, runs and prints 0

b)

The snippet compiles, runs and prints 1

c)

The snippet does not compile

d)

The snippet compiles and runs but does not print anything

13.

What will be the output of the following Java code snippet running with “java demo I write java code”?

a)

The snippet compiles, runs and prints “I code”

b)

The snippet compiles, runs and prints “java code”

c)

The snippet compiles, runs and prints “demo code”

d)

The snippet compiles, runs and prints “java demo”

14.

Which four options describe the correct default values for array elements of the types indicated?

a)

1, 2, 3, 4

b)

1, 3, 4, 5

c)

2, 4, 5, 6

d)

3, 4, 5, 6

15.

Which one of these lists contains only Java programming language keywords?

a)

class, if, void, long, Int, continue

b)

try, virtual, throw, final, volatile, transient

c)

goto, instanceof, native, finally, default, throws

d)

strictfp, constant, super, implements, do

e)

byte, break, assert, switch, include

16.

Which is a valid keyword in java?

a)

interface

b)

string

c)

Float

d)

unsigned

17.

Which three are valid declarations of a float?

a)

1, 2, 4

b)

2, 3, 5

c)

1, 3, 6

d)

2, 4, 6

18.

What is the numerical range of a char?

a)

-128 to 127

b)

-(215) to (215) - 1

c)

0 to 32767

d)

0 to 65535

19.

Which of the following are legal lines of Java code?

a)

1 and 2

b)

All statements are correct

c)

2 and 3

d)

3 and 4

20.

Which of these is necessary condition for automatic type conversion in Java?

a)

The destination type is smaller than source type

b)

The destination type is larger than source type

c)

The destination type can be larger or smaller than source type

d)

None of the mentioned

21.

If an expression contains double, int, float, long, then the whole expression will be promoted into which of these data types?

a)

long

b)

int

c)

double

d)

float

22.

Which of these coding types is used for data type characters in Java?

a)

ASCII

b)

UNICODE

c)

ISO-LATIN-1

d)

None of the mentioned

23.

Which of these values can a boolean variable contain?

a)

True & False

b)

0 & 1

c)

true

d)

Any integer value

24.

Which one is a valid declaration of a boolean?

a)

boolean b1 = 1;

b)

boolean b2 = ‘false’;

c)

boolean b3 = false;

d)

boolean b4 = ‘true’

25.

What will be the output of the following Java code?

a)

0

b)

1

c)

true

d)

false

26.

what is the output of the following code?


byte b=10;

b+=20;

System.out.println(b)

a)

10

b)

30

c)

0

d)

error: incompatible types

27.

what is the output of the following code?


byte b=10;

b=b+30;

System.out.println(b)

a)

10

b)

40

c)

0

d)

error: incompatible types

28.

Which of these is not a bitwise operator?

a)

&

b)

&=

c)

|=

d)

<=

29.

What will be the output of the following Java program?

a)

7 2

b)

7 7

c)

7 5

d)

5 2

30.

What will be the output of the following Java program?

a)

256 0

b)

0 64

c)

64 0

d)

0 256

31.
Of the following if statements, which one correctly executes three instructions if the condition is true?
a)
if (x < 0)  
a = b * 2;
 
y = x;
 
 z = a – y;
b)
{
   if (x <0)
   a = b*2;
   y=x;
   z=a-y;
}
c)
if { (x < 0) 
a = b * 2;  
y = x;  
 z = a – y;
}
d)
if (x <0)
{
a = b*2;
y = x;
z = a - y;
}
32.
Which of the sets of statements below will add 1 to x if x is positive and subtract 1 from x if x is negative but leave x alone if x is 0?
a)
if (x > 0)  x++;
else x--;
b)
if (x > 0) x++;
else if (x < 0) x--;
c)
if (x > 0) x++;
if (x < 0) x--;
else x = 0;
d)
if (x == 0) x = 0;
else x++;
x--;
33.
Given the nested if-else structure above, If x is currently 0, a = 5 and b = 5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
34.
Given the nested if-else structure above, if x is currently 0, a = 0 and b = -5, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
4
35.
Given the nested if-else structure above, if x is currently 0, a = 1 and b = -1, what will x become after the above statement is executed?
a)
0
b)
2
c)
3
d)
5
36.
Consider the above code that will assign a letter grade of ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ depending on a student’s test score.  
a)
This code will work correctly in all cases
b)
This code will work correctly only if grade >= 60
c)
This code will work correctly only if grade < 60
d)
This code will work correctly only if grade < 70
37.
Assume that count is 0, total is 20 and max is 1.  The above statement will do which of the following? 
a)
The condition short circuits and the assignment statement is not executed
b)
The condition short circuits and the assignment statement is executed without problem
c)
The condition does not short circuit causing a division by zero error
d)
The condition short circuits so that there is no division by zero error when evaluating the condition, but the assignment statement causes a division by zero error
38.
What is wrong, logically, with the following code?if (x > 10) System.out.println("Large");else if (x > 6 && x <= 10) System.out.println("Medium");else if (x > 3 && x <= 6) System.out.println("Small");else System.out.println("Very small");
a)
There is no logical error, but there is no need to have (x <= 10) in the second conditional or (x <= 6) in the third conditional
b)
There is no logical error, but there is no need to have (x > 6) in the second conditional or (x > 3) in the third conditional
c)
The logical error is that no matter what value x is, “Very small” is always printed out
d)
The logical error is that no matter what value x is, “Large” is always printed out
39.
Consider the above outline of a nested if-else structure which has more if clauses than else clauses.  Which of the statements below is true regarding this structure?
a)
syntactically it is invalid to have more if clauses than else clauses
b)
statement2 will only execute if condition1 is false and condition2 is false
c)
statement2 will only execute if condition1 is true and condition2 is false
d)
statement2 will only execute if condition1 is false, it does not matter what condition2 is
40.
Assume that x and y are int variables with x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A', and examine the above conditions:
a)
All 4 Conditions are true
b)
Only Condition 2 is true
c)
Condition 2 and Condition 4 are true only
d)
Conditions 2, 3 and 4 are all true, Condition 1 is not
41.
If x is an int where x = 1, what will x be after the above loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
42.
If x is an int where x = 0, what will x be after the following loop terminates?
a)
64
b)
100
c)
128
d)
None of the above, this is an infinite loop
43.
Given the above code, where x = 0, what is the resulting value of x after the for-loop terminates?
for(int i=0;  i<5; i++)    
            x += i;
a)
4
b)
5
c)
10
d)
15
44.
How many times will the above loop iterate?
a)
0
b)
9
c)
10
d)
11
45.
Given two String variables, s1 and s2, to determine if they are the same length, which of the following conditions would you use?
a)
(s1.equals(s2))
b)
(s1.length( ).equals(s2))
c)
(s1.length( ).equals(s2.length( ))
d)
(s1.length( ) == s2.length( ))
46.
Given a String, s, which is assumed to have at least one character in it, which of the following conditions would determine if the first character of the String is the same as the last character?
a)
(s.charAt(0) == s.charAt(s.length( )))
b)
(s.charAt(1) == s.charAt(s.length( )))
c)
(s.charAt(0) == s.charAt(s.length( ) - 1))
d)
(s.charAt(0) == s.charAt(s.length( ) + 1))
47.
The above nested loop structure will execute the inner most statement (x++) how many times?
a)
100
b)
200
c)
10000
d)
20000
48.
Consider the following paint method above. This paint method will draw several bars (sort of like a bar graph).  How many bars will be displayed?
a)
4
b)
5
c)
20
d)
200
49.
Considering the (incomplete) code above, x is probably 
a)
a primitive type
b)
a String
c)
an iterator
d)
a random number generator
50.

Base on the code block below what will be the output to the console?


int number =11;


if(number >=10) {

System.out.print("New York");

}

else if(number<=5){

System.out.print("California");

}

else System.out.print("Texas");

a)

New York

b)

California

c)

Texas

d)

Nothing

51.

Base on the code block below what will print to the console?


var n =5;


for(int i=1;i<=n;i++){

System.out.println(i);

}

a)

1

2

3

4

5

b)

5

4

3

2

1

c)

1

2

3

4

d)

4

3

2

1

52.

Of the following if statements has the correct syntax for executing three instructions if the condition is true?

a)

if (x < 0)

a = b * 2;

y = x;

z = a – y;

b)

{

if (x <0)

a = b*2;

y=x;

z=a-y;

}

c)

if { (x < 0)

a = b * 2;

y = x;

z = a – y;

}

d)

if (x <0)

{

a = b*2;

y = x;

z = a - y;

}

53.

Which one of these control flow statements have three parameters to it?

a)

switch

b)

while

c)

do-while

d)

for

54.

Which one of these control flow statements does not loop

a)

switch

b)

while

c)

do-while

d)

for

55.

Base on the code block below what will be the output to the console?


int number =2;


if(number >=10) {

System.out.print("New York");

}

else if(number<=5){

System.out.print("California");

}

else System.out.print("Texas");

a)

New York

b)

California

c)

Texas

d)

Nothing

56.

Base on the code block below what will be the output to the console?


int number =8;


if(number >=10) {

System.out.print("New York");

}

else if(number<=5){

System.out.print("California");

}

else System.out.print("Texas");

a)

New York

b)

California

c)

Texas

d)

Nothing

57.

Base on the code block below what will print to the console?


var n =5;


for(int i=1;i<n;i++){

System.out.println(i);

}

a)

1

2

3

4

5

b)

5

4

3

2

1

c)

1

2

3

4

d)

4

3

2

1

58.

Base on the code block below what will be the output to the console?


int n=5;

int c=0;

while(n>=0&&c<=5) {

System.out.println(c+":"+n);

c++;

n--;

}

a)

1:1

2:2

3:3

4:4

5:5

b)

1:4

2:3

3:2

4:1

c)

0:5

1:4

2:3

3:2

4:1

5:0

d)

0:0

1:1

2:2

3:3

4:4

5:5

59.

Which control flow statement provides a compact way to iterate over a range of values.; by allowing a (initialization; termination; increment) condition in its statement.

a)

While

b)

Switch

c)

If

d)

For

60.

Base on the code block below what will print to the console?


int x=10;

do {System.out.println(x*2);

x--;}

while(x>10);

a)

20

b)

infinite loop

c)

200

d)

20000000

61.

What will be the output of the program?

a)

12 15

b)

3 4 5 3 7 5

c)

15 15

d)

3 7 5 3 7 5

62.

What will be the output of the program?

a)

true

b)

false

c)

Compilation fails

d)

An exception is thrown at runtime

63.

What will be the output of the program?

a)

small

b)

huge

c)

tiny

d)

Compilation fails

64.

What will be the output of the program?

a)

small

b)

huge

c)

tiny

d)

Compilation fails

65.

What will be the output of the program?

a)

5 3

b)

6 3

c)

6 4

d)

5 2

66.

What will be the output of the program?

a)

5 3

b)

8 3

c)

8 2

d)

8 5

67.

What will be the output of the program?

a)

0

b)

7

c)

8

d)

14

68.

What will be the output of the program?

a)

ok

b)

ok dokey

c)

dokey

d)

No output is produced

69.

What will be the output of the program?

a)

9 7 7 foo 7 7foo

b)

72 34 34 foo34 34foo

c)

9 7 7 foo34 34foo

d)

72 7 34 foo34 7foo

70.

What will be the output of the program?

a)

7 7

b)

14 0

c)

7 14

d)

14 14

71.

What will be the output of the program?

a)

null null 42

b)

0 0 42

c)

0 42 42

d)

0 0 0

72.

What will be the output of the program?

a)

count = 0

b)

count = 2

c)

count = 3

d)

count = 4

73.

What will be the output of the program?

a)

2

b)

4

c)

8

d)

16

74.

What is the output?

a)

There is a syntax error on line 1.

b)

There are syntax errors on lines 1 and 6.

c)

There are syntax errors on lines 1, 6, and 8.

d)

There is a syntax error on line 6.

75.

What will be the output of the program?

a)

j = -1

b)

j = 0

c)

j = 1

d)

Compilation fails.

76.

What will be the output of the program?

a)

i = 5 and j = 5

b)

i = 6 and j = 5

c)

i = 5 and j = 6

d)

i = 6 and j = 4

77.

What will be the output of the program?

a)

0 2 4

b)

0 2 4 5

c)

0 1 2 3 4

d)

Compilation fails.

78.

What will be the output of the program?

a)

a

b)

b

c)

c

d)

d