FREE! Click here to Join FunTrivia. Thousands of games, quizzes, and lots more!
Quiz about Java Programming
Quiz about Java Programming

Java Programming Trivia Quiz


What will the following sections of Java code do?

A multiple-choice quiz by nemesis. Estimated time: 4 mins.
  1. Home
  2. »
  3. Quizzes
  4. »
  5. Science Trivia
  6. »
  7. Computers
  8. »
  9. Software and Programming

Author
nemesis
Time
4 mins
Type
Multiple Choice
Quiz #
204,270
Updated
Dec 03 21
# Qns
10
Difficulty
Average
Avg Score
7 / 10
Plays
1881
- -
Question 1 of 10
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);
Hint


Question 2 of 10
2. What would the following fragment of code output?
int sum = 5;
sum = sum + sum *5/2;
System.out.println(sum);
Hint


Question 3 of 10
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[]
Hint


Question 4 of 10
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);
Hint


Question 5 of 10
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?
Hint


Question 6 of 10
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?
Hint


Question 7 of 10
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?
Hint


Question 8 of 10
8. Which of the following statements about Java methods is false? Hint


Question 9 of 10
9. Which of the following is a constant? Hint


Question 10 of 10
10. Which of the following shows the construction of an object? Hint



(Optional) Create a Free FunTrivia ID to save the points you are about to earn:

arrow Select a User ID:
arrow Choose a Password:
arrow Your Email:




Most Recent Scores
Nov 21 2024 : GoodVibe: 6/10
Nov 09 2024 : Guest 73: 9/10

Score Distribution

quiz
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);

Answer: Answer is 3

This code will print out whatever the variable "mesg" and the variable "sum" is. In the case mesg is "Answer is" and sum is 1+" = 3 therefore the sum value is "3". The computer will display "Answer is 3"
2. What would the following fragment of code output? int sum = 5; sum = sum + sum *5/2; System.out.println(sum);

Answer: 17

Remember in Java the order, or precedence, of the way sums are worked out:
The fact that:
() are worked out first,
then *, /and % (times, divide, and modulus (the remainder left after a sum)),
then + and - (addition and subtraction),
finally the = (assignment).
Also as the "sum" variable is an int type, nothing after the decimal place when doing the division will be stored.
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[]

Answer: 3 and 4

The "totall" in the println statement isn't a compile error; it will just be a spelling error when the program is run (This rules out 1 as an option.) It doesn't matter where the [] is on the String args[] statement (This rules out 2 as an option.) However the positioning of the ";"'s are very important.
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);

Answer: 370

count would be 30 x 5 = 150
limit would be 25 - 5 = 20
total would be 200 + 150 + 20 = 370
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?

Answer: 8

As both the weekTotal and the dayTotal variable would activate the loop, (remember || means or) the value of weekTotal would be:
daytotal (4)
times limit (2)
which equals 4 x 2 = 8
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?

Answer: "Javac"

s1 is str1(Java) concatenated with the letter c, resulting in the string "Javac" as no spaces have been added between the two strings.
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?

Answer: " Javaprogram" (first character is a space)

the += function adds str1 (Java) onto the end of what's already in the s4 making it " Java", this is the value of s4. s5 is s4 with "program" added on the end, making the string " Javaprogam"
8. Which of the following statements about Java methods is false?

Answer: methods are used to avoid putting comments in code

There is no way to avoid putting comments in code. All code should be commented so other people can understand your work.
9. Which of the following is a constant?

Answer: final int STOP = -999;

A constant is defined by the "final" in the declaration. A constant cannot be changed within the program.
10. Which of the following shows the construction of an object?

Answer: ConsoleWindow c = new ConsoleWindow();

The others are examples of assignment statements.

Hope you enjoyed this quiz :)
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.
12/22/2024, Copyright 2024 FunTrivia, Inc. - Report an Error / Contact Us