Font size
WorksheetsJava Fundamentals
Total questions: 47
Worksheet time: 3hrs 49mins
When programming, you have to use Data Types. Which of the following refers to Strings, classes, and arrays?
Primitive
Reference
Arrays
Boolean
The main() method is not mandatory for a Java program
True
False
Which one is a Java valid variable name?
Sum Grades
static
public
FindAverage
which statement is correct?
int Var
boolean 15;
double var123;
int min=0
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]);
}
2
4
6
8
10
2,4,6,8,10
6
6
6
6
6
Nothing
3 + 6 / 2 * 3 - 1 + 2 =
14.5
13
5
13.0
num= 2;
x=num++;
++num;
After performing the above statements, the values of x and num are
x= 2
num= 3
x = 3
num = 4
x= 2
num = 4
x= 3
num= 3
nextInt() is used to read user's numerical input
True
False
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;
I only
II only
III only
I and III only
What output will be produced by:
System.out.print("\\* This is not\n a comment *\\"):
\* This is not a comment *\
* This is not
a comment *
\\* This is not
a comment *\\
\* This is not
a comment *\
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?
double answer = (double) 13 / 5;
double answer = 13 / (double) 5;
double answer = 13.0 / 5;
double answer = (double) (13 / 5);
What value is stored in result if:
int result = 13 - 3 * 6 / 4 % 3;
11
13
0
12
Which of the following will evaluate to true only if boolean expressions
A, B, and C are all false?
!A && !(B && !C)
!(A || B || C)
!(A && B && C)
!A || !(B || !C)
Assume that a and b are integers. The boolean expression
!( a <= b) && (a * b > 0)
will always evaluate to true given that
a > b
a < b
a > b and b > 0
a > b and b < 0
Name the data type: "true"
boolean
char
String
double
Name the data type: 1.0
double
int
char
boolean
Name the data type: '3'
int
char
String
None of the above
What data type would you use for storing the number of students in a class?
boolean
String
double
int
What data type would you use for storing the average test score in a class?
double
String
int
char
How would you declare a variable storing a person's name?
string name = "Jeff";
name String = "Jeff";
String name = Jeff;
String name = "Jeff";
How would you declare a variable storing the tax rate?
int taxRate = 5.1;
taxRate = "5.1";
double taxRate = 5.1;
double taxRate = "5.1";
Which expression evaluates to true?
false == false
true != true
4 < 4
true == true != true
Which expression evaluates to false?
15 % 3 < 2
3 * 3 % 2 <= 0
false == false
false == false == false == false
Which declaration will throw an error?
double num = 8;
int averageGrade = 89.7;
boolean done = false;
String done = "true";
What prints out "I love Java" successfully?
System.out.println(I love Java);
Systemoutprintln("I love Java);
System.out.println("I love" + " Java");
System.out.println("I love Java")
Which will legally declare, construct, and initialize an array?
int [] myList = {"1", "2", "3"};
int [] myList = (5, 8, 2);
int myList [] [] = {4,9,7,0};
int myList [] = {4, 3, 7};
What will be the output of the program?
true
false
Compilation fails
An exception is thrown at runtime
What will be the output of the program?
5 3
6 3
6 4
5 2
What will be the output of the program?
5 3
8 3
8 2
8 5
What will be the output of the block of code in the main() method?
j = -1
j = 0
j = 1
Compilation fails.
What will be the output of the the block of code in the main() method?
i = 5 and j = 5
i = 6 and j = 5
i = 5 and j = 6
i = 6 and j = 4
What will be the output of the the block of code in the main() method?
0 2 4
0 2 4 5
0 1 2 3 4
Compilation fails.
What will be the output of the the block of code in the main() method? (NOTE: This is a "trick" question!)
a
b
c
d
Consider the following code segment:
Line 1: int a = 10;
Line 2: double b = 10.7;
Line 3: int d = a + b;
Line 3 will not compile in the code segment above. With which of the following statements could we replace this line so that is compiles?
I. int d = (int) a + b;
II. int d = (int) (a + b);
III. int d = a + (int) b;
II
III
I or III
II or III
Consider the following code segment:
int var = 12;
var = var % 7;
var = var - 1;
System.out.println(var);
What is printed as a result of executing the code segment?
1
2
4
5
Consider the following code segment:
int count = 5;
double multiplier = 2.5;
int answer = (int)(count * multiplier);
answer = (answer * count) % 10;
System.out.println(answer);
What is printed as a result of executing the code segment?
0
2.5
6
12.5
Assume that x, y, and z have been declared as follows:
boolean x = true;
boolean y = false;
boolean z = true;
Which of the following expressions evaluates to true?
x && y || z && y
!(x && y) || z
!(x || y) && z
x && y || !z
Function of the Scanner class to accept a float literal from the user is
nextInt( )
hasNext( )
next( )
nextFloat( )
Absence of (a) ________ in switch..case leads to fallthrough
switch
case
default
break
Find the output of,
int x =4;
while(x>5)
{
System.out.println(x);
x++;
}
6
7
8... infinity
1
2
3
4
No Output
4
5
How do you insert COMMENTS in Java code?
/* This is a comment
# This is a comment
// This is a comment
/* This is a comment */
The value of a string variable can be surrounded by single quotes.
True
False
Which method can be used to return a string in upper case letters?
touppercase()
toUpperCase()
upperCase()
toUppercase()
Which operator can be used to compare equality between two primitive values?
><
=
==
<>
How do you start writing an if statement in Java?
if (x > y)
if x > y then:
if x > y:
if (x>y):
Which statement about choosing variable names is FALSE?
Variable names can contain letters, digits, underscores, and dollar signs.
Variable names are NOT case sensitive
Reserved words (like Java keywords, such as int or String) cannot be used as variable names
Variable names can also begin with $ and _
