The BASE for JAVA LEARNING
by
PRO
FE S
S OR

QUIZ for SECTION IV
To find an answer, click on the desired question.
QUESTION 1: Write a program to multiply a 'double' times
an 'int' and make the result an 'int'. Display the result.
Would you have to do explicit casting ?
QUESTION 2: Given the following local variables and their values:
double a = 23.31 ;
double b = 122.9 ;
What is the value of 'result' below for each of the lines of code a through d ?
a) double result = (int)a + b;
b) int result = (int)a + (int)b;
c) int result = (int)(a + (int)b);
d) int result = (int)(a + b);
QUESTION 3: What arithmetic operator would you use to find if an integer is
divisible by a number ?
QUESTION 4: Given the following code, what number is displayed ?
class Quiz
{
public static void main(String[] args)
{
int number = 0;
number+=2;
System.out.println("number = " + ++number);
}
}
QUESTION 5: If int i = 4. what is ~i ?
QUESTION 6: If flag = false, what is !flag ?
QUESTION 7: Given the following code, what would be displayed ?
class Quiz
{
int year;
public static void main(String[] args)
{
boolean flag1 = true;
boolean flag2 = true;
if (flag1 && (flag2 = false)) // an assignment in this evaluation
{
flag1 = false;
}
System.out.println("flag1 = " + flag1);
System.out.println("flag2 = " + flag2);
}
}
Click your Back Button on your Browser to go back to the QUIZ Section.
ANSWER to QUESTION 1:
class Quiz
{
public static void main(String[] args)
{
double a = 2.454;
int b = 45;
int result = (int)(a * b); // casting is explicit
System.out.println(result); // displays 110
}
}
Yes, casting is explicit.
Click your Back Button on your Browser to go back to the QUIZ Section.
ANSWER to QUESTION 2: a) 145.9
b) 145
c) 145
d) 146
Click your Back Button on your Browser to go back to the QUIZ Section.
ANSWER to QUESTION 3: % The modulo operator
Click your Back Button on your Browser to go back to the QUIZ Section.
ANSWER to QUESTION 4: number = 3
Click your Back Button on your Browser to go back to the QUIZ Section.
ANSWER to QUESTION 5: -5
Click your Back Button on your Browser to go back to the QUIZ Section.
ANSWER to QUESTION 6: !flag = true;
Click your Back Button on your Browser to go back to the QUIZ Section.
ANSWER to QUESTION 7: flag1 = true
flag2 = false;
flag2 was assigned to false when the 'if' expression was evaluated.
The 'if' construct was not implemented as it was evaluated to false, and therefore flag1 remained true.
Click your Back Button on your Browser to go back to the QUIZ Section.
Click here to go to the JAVA Mini-Course page