This document contains a practice test with 25 multiple choice and code writing questions covering Java fundamentals like variable declaration, operators, input/output, and math functions. The test covers topics like declaring and initializing variables, precedence of operators, input from the Scanner class, and methods from the Math class like round, floor, ceil, max, abs, pow, and more. Sample code is provided to demonstrate expected behavior.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
44%(9)44% found this document useful (9 votes)
2K views3 pages
Test TL 7 Answer Key
This document contains a practice test with 25 multiple choice and code writing questions covering Java fundamentals like variable declaration, operators, input/output, and math functions. The test covers topics like declaring and initializing variables, precedence of operators, input from the Scanner class, and methods from the Math class like round, floor, ceil, max, abs, pow, and more. Sample code is provided to demonstrate expected behavior.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3
.
H\WR7HVWRQ/HVVRQ(PSKDVL]LQJ
Test, Lesson 07-4
1. Write a VLQJOH line of code to accomplish the following:
Declare p, q, and r to be of type int. Initialize p to a value of 3 and r to a value of 16. LQWS TU 2. Which of the following is legal assuming w has been declared as an int type. a. 59 = w; E Z c. Both a and b d. None of these 3. What is output by the following code? int x = 22; int y = 6; System.out.println(x % y); // 4. Write the skeleton code for a class called Bozo. This includes the skeleton of the class plus the skeleton of the main method inside it. SXEOLFFODVV%R]R ^
SXEOLFVWDWLFYRLGPDLQ6WULQJDUJV>@
^
` ` 5. Consider the following code fragment:
Scanner kbReader = new Scanner(System.in); System.out.print(What is your code? ); ! //this line of code stores in m whatever is typed in from the keyboard in response to the above prompt System.out.println(Your code is + m + .);
Now suppose when this code is run and the program finishes, the output screen appears as follows: What is your code? 1234 Your code is 1234. To achieve all this, what would be a OHJDO replacement for ! above? a. String m = kbReader.nextLine( ); b. int m = kbReader.nextInt( ); c. double m = kbReader.nextDouble( ); G $OORIWKHDERYH e. None of these
Test, Lesson 07-5
6. What is output by the following code? System.out.println(4 7 * 4 / 3 + 2); // 7. Give at least three different code examples for how to decrement the int variable j by 1. M M M M M
8. What is output by the following code?
int q = 15; int p = 1 + (++q); System.out.println(The value of p is + p); System.out.println(The value for q is + q);
7KHYDOXHRISLV
C 7KHYDOXHRITLV 9. What is output by the following code? System.out.println(7 / 3.0); // System.out.println(7 / 3); // 10. What is output by the following code? int p = 9, q = 5; System.out.println( (double)(p / q) ); // System.out.println( (double)p / q ); // 11. Write a single line of code that will store the result of the following formula in the double precision variable jk. ___________ 5.012.33 + S GRXEOHMN 0DWKVTUW0DWKSRZ0DWK3, 12. What is the value of Math.round(-122.83)?
13. What is the value of Math.floor(1023.999)?
14. What is the value of Math.ceil(-484.22)?
15. What is the value of Math.max(-100,-200)?
16. What is the value of Math.abs( 52 * (-1) )?
17. What is the value of (int)(10.0 / 2.0)?
Test, Lesson 07-6
18. Modify the second line of the following code with a cast so as to make it legal. double d = 122.0; int x = d; LQW[ LQWG
19. Rewrite x = x * (p-j); using a compound operator. [
SM 20. Write a line of code that creates a constant called DUDE and initializes it to 167.2. ILQDOGRXEOH'8'( 21. Assume you already have a Scanner object called rdr. Write a line of code that will bring in a floating point number from the keyboard and store it in a variable called pooky. GRXEOHSRRN\ UGUQH[W'RXEOH 22. What is the value of Math.pow(2,3)?
23. Write a line of code in which you divide the double precision number p by an integer variable k. Type cast the double so that strictly integer division is done. Store the result in pp, an integer. LQWSS LQWSN 24. What does the following print? double fruit_count = 22.05; System.out.println(++fruit_count);
25. What does the following print? double cash =1034.89; System.out.println(cash++);