Quiz Answer Key and Fun Facts
1. What would be printed out when the following fragment of code was executed?
String mesg = "Answer is ";
int sum = 1 + 2;
System.out.println(mesg + sum);
2. What would the following fragment of code output?
int sum = 5;
sum = sum + sum *5/2;
System.out.println(sum);
3. Study the following Java class:
public class outExample
{
public static void main(String args[]);
{
int total = 0;
total = 15 + 20/3
System.out.println("totall" + total);
}
}
which of the following are correct compilation errors of the above code?
1) total spelt incorrectly in the println statement
2) String args[] should be String[] args
3) a semi-colon is missing from the second line of the main method (total =...)
4) the semi-colon should NOT be after the line ending with args[]
4. Consider the following fragment of Java code:
int limit = 25;
int count = 30;
int total = 200;
count *=5;
limit -=5;
total +=count + limit;
System.out.println("total =" + total);
5. Consider the following fragment of code
int weekTotal, dayTotal, limit;
weekTotal = 2;
dayTotal = 4;
limit = 2;
if(weekTotal >= 2 || dayTotal >= 4)
weekTotal = dayTotal*limit;
System.out.println("value of weekTotal is " + weekTotal);
What value of weekTotal will be printed out?
6. Consider the following code fragment:
String str1 = "Java";
String str2 = "Java program";
String str3 = "program";
char c = ' ';
String s1 = str1 + str3;
String s2 = str1 + "c";
String s3 = str1 + c;
String s4 = " ";
s4 += str1;
String s5 = s4 + str3;
What would be the value of string s2?
7. Consider the following code fragment
String str1 = "Java";
String str2 = "Java program";
String str3 = "program";
char c = ' ';
String s1 = str1 + str3;
String s2 = str1 + "c";
String s3 = str1 + c;
String s4 = " ";
s4 += str1;
String s5 = s4 + str3;
What would be the value of string s5?
8. Which of the following statements about Java methods is false?
9. Which of the following is a constant?
10. Which of the following shows the construction of an object?
Source: Author
nemesis
This quiz was reviewed by FunTrivia editor
crisw before going online.
Any errors found in FunTrivia content are routinely corrected through our feedback system.