AP Computer Science A Unit 2. Exercises A common standard is 24-bit color where 8 bits are used to represent the amount of red light, 8 bits for green light, and 8 bits for blue light. It is the combination of these three colors that generates every other color you see on a monitor. 1. How many different levels of red can be represented using 8 bits? 2. How many different colors can be represented using 24 bits? Y2K refers to a computer problem from the previous century. In the early days of computers, memory (whether for storage or running a program) was very expensive. So, when recording dates, only the last two digits were saved. For example, the year 1985 would be saved as 85. This worked well enough for most programs at first but with the approach of the year 2000, it needed to be fixed; otherwise the year 2000 would be indistinguishable from the year 1900 (as both would be saved as 00). 3. How many bits are necessary to save a number from 0 to 99? 4. How many bits are necessary to save a number from 0 to 9999? 5. An Arduino Uno microcontroller can detect a voltage between 0 and 5 volts. How many bits does it use to represent the voltage if 1023 indicates 5 volts and 0 indicates 0 volts? Translate each binary number into a base ten number. 6) 1010 2 7) 0010 0001 2 8) 1111 2 9) 1000 0000 2 10) 1111 1111 2 11) 1000 1000 2 Translate each hexadecimal number into a base ten number 12) 31 16 13) 2F 16 14) 5D 16 15) A5 16 16) 59 16 17) 12 16 1
Translate each number into a base 10 number. If a number is incorrectly represented, and therefore cannot be translated, write INVALID. 18) 22 3 19) 80 7 20) 31 5 21) 123 4 22) 272 8 23) 55 6 Translate each base ten integer into a binary number. 24) 67 25) 100 26) 8 27) 12 28) 50 29) 88 Translate each base ten number into a hexadecimal number. 30) 30 31) 123 32) 8 33) 64 34) 54 35) 75 36) Convert this integer from base 10 to base 5 24 37) Convert this integer from base 10 to base 9 18 38) Convert this integer from base 10 to base 7 60 39) Convert this integer from base 10 to base 3 23 2
Any binary number can be easily converted to hexadecimal, and vice versa. Since 4 bits can represent a value between 0 and 15, each group of 4 bits can converted to the corresponding hex number. For example 1101 0100 = D4 because 1101 = D and 0100 = 4 40) This is why hex numbers are so common. For example, going back to the problems at the beginning of this packet, which is the easier way to specify a color? a) B16FCA b) 1011 0001 0110 1111 1100 1010 Translate each binary number into a hexadecimal number. 41) 0010 1011 2 42) 1010 0001 2 43) 1111 1101 2 44) 1011 0001 2 45) 0000 1011 1011 2 46) 1001 1001 1000 2 Translate each hexadecimal number into a binary. 47) FE 16 48) 200 16 49) 8 16 50) 4F0 16 51) A0 16 52) B03 16 Run the following program in BlueJ to answer questions 53 to 56. import javax.swing.jcolorchooser; import java.awt.color; public class MyColorChooser { public static void main(string[] args) { Color c1 = Color.ORANGE; Color c2 = JColorChooser.showDialog( null,"pick a Color", c1 ); System.out.println( c2.tostring() ); 53. Click on the RGB tab and set Red to 255, Green to 255, Blue to 0, and Alpha to 255. What background color of the sample text?. 3
54. Change the color code to 00FF00. What is the background color of the sample text? Why? 55. If the color code is 000000, what is the background color of the text? 56. If color1 is CC0000 and color2 is 990000, which color is darker? Try to figure this out first then check by entering those numbers. 57. Java code is saved in a text file that has a.class extension. True False 58. To run Java byte code on a particular computer, you must install a java compiler. True False 59. The JVM detects run-time errors. True False 60. The JVM detects logic errors. True False 61. The Java compiler translates text into Byte code. True False 62. The JVM translates byte code into the machine language of the particular device it is installed on. True False 63. A bit consists of 8 bytes. True False 64. The same Java class files can be run on a variety of computers (e.g. those running Windows, Linux, etc.) True False 65) Is the following binary number even or odd? 0111 0100 1011 100 1100 1011 1000 1110 1010 0101 There is a way to figure this out without converting it to a base 10 integer. 66) Let s say you ve written a program that looks something like this: public class Light{ public static void main(string[] args) { // some great code and I say, email me the code. Which file(s) should you send me? a) Light.java b) Light.ctxt c) Light.class d) All of the above files e) Any of the first three files. 4
67) ASCII (American Standard Code for Information Interchange), started in the 1960 s, was a very common 7 bit encoding scheme for letters, numbers, etc. What was the total number of characters that it could represent? Numbers 65 to 90 represented the uppercase letters A to Z. Numbers 97 to 112 represented the lowercase letters a to z. Convert 65 (base ten) to binary Convert 97 (base ten) to binary How many bits have to be changed to convert a lowercase letter to an uppercase letter (or vice versa)? 68) Numeric literals can be represented in binary form by putting 0b or 0B in front (that s a zero, not an O). However, the value of an int is still displayed in base 10. What is displayed? 69) Putting 0x, or 0X, before a numeric literal means that it is a hexadecimal value. What is displayed? int x = 0b11; int y = 0b11; int c = x + y; System.out.println( c ); int x = 0xF; int y = 0x2; int c = x + y; System.out.println( c ); 70) What is displayed? int x = 0b1110; int y = 0x12; int c = x + y; System.out.println( c ); 71) What is displayed? int num1 = 0b0010; int num2 = 0x10; int result = num1 * num2; System.out.println( result ); Note: Curly brackets { are optional if there is only one statement associated with the if (or ) statement.) 72. If the user enters 82, what is displayed? 73. If the user enters 2, what is displayed? Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextint(); if ( x < 7 ) { System.out.print( "A" ); System.out.print( "B" ); System.out.print( "C" ); 5
74. If the user enters 10, what is displayed? 75. If the user enters 12, what is displayed? 76. Are the curly braces required in this code? 77. If the user enters 10, what is displayed? 78. If the user enters 4, what is displayed? 79. If the user enters 2, what is displayed? 80. If x has an initial value of 33, what is its final 81. If x has an initial value of 62, what is its final Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextint(); System.out.print( "AA" ); if ( 11 >= x ) { System.out.print( "BB" ); System.out.print( "CC" ); Scanner get = new Scanner(System.in); System.out.print( "Enter a number: " ); int x = get.nextint(); if ( x!= 10 ) x = x + 3; if ( x > 7 ) x = x + 12; x = x - 1; System.out.println( x ); // x is declared and assigned a value if ( x > 30 && x <= 50 ) x = x + 10; x = x - 4; if ( x < 40 && x > 60 ) x = x + 2; 82. If x has a value of -5 and y has a value of 63, what is displayed? 83. If x has a value of 47 and y has a value of 47, what is displayed? 84. Select the TRUE statement. a) H is never printed. b) H is always printed. c) H is only printed sometimes. // x and y are declared and initialized if ( x > 30 y >= 60 ) System.out.print( "G" ); if ( x < 100 x > 40 ) System.out.print( "H" ); if ( y < 10 y > 50 ) System.out.print( "K" ); 6
85. If y has an initial value of 12, what is displayed? 86. If y has an initial value of 26, what is displayed? 87. If y has an initial value of 7, what is displayed? 88. The code to the right does not compile. The is highlighted and the error message is: without if Why? 89. Given that the above error is fixed by the addition of two characters, what will be displayed if the user enters 12? 90. If x has an initial value of -8, what is its final 91. If x has an initial value of 9, what is its final 92. If x has an initial value of 15, what is its final 93. If x has an initial value of 22, what is its final 94. If x has a final value of 6, what was its initial Scanner x = new Scanner( System.in ); System.out.println( "Number?" ); int y = x.nextint(); if ( y > 10 ) y += 10; if ( y > 20 ) y++; System.out.println( y ); Scanner read = new Scanner( System.in ); System.out.println( "Number?" ); int a = read.nextint(); if ( a > 10 && a < 20 ) a = 2 * a; System.out.println( a ); a--; System.out.println( a ); // x is declared and assigned a value if ( x > 10 x < 20 ) x = x + 2; if ( x < 10 x > 20 ) x = x + 3; // x is declared and assigned a value if ( x > 10 && x < 20 ) x = x + 2; if ( x < 10 && x > 20 ) x = x + 3; if ( x > 10 && x > 20 ) x = x + 10; 95. If the user enters "38", what is displayed? 96. If the user enters -4, what is displayed? 97. If the user enters 99, what is displayed? Scanner read; read = new Scanner(System.in); System.out.print( "Enter a number: " ); int num = read.nextint(); if ( num < 13) { System.out.println( "ZZ" ); if ( num < 20) { System.out.println( "YY" ); if ( num < 40 ) { System.out.println( "XX" ); System.out.println( "WW" ); 7
98. If k has an initial value of 13, what is its final 99. If k has an initial value of 22, what is its final 100. If k has an initial value of 4, what is its final // k is declared and assigned a value if ( k < 5 ) k += 2; if ( k < 10 ) k += 5; if ( k < 20 ) k += 6; 101. If k has an initial value of 10, what is its final 102. If k has an initial value of 30, what is its final 1 2 3 4 // k is declared and assigned a value if ( k < 11 ) k += 4; if ( k < 40 ) k++; 103. Lines 7 and 8 can be deleted without changing how the code runs. TRUE FALSE 5 6 7 8 if ( k > 11 ) k = k - 2; if ( k > 22 ) k -= 4; 104. If x has an initial value of 8, what is its final 105. If x has an initial value of 11, what is its final 106. If x has an initial value of 19, what is its final // x is declared and assigned a value if ( x > 10 ) x = x + 2; if ( x < 5 ) x++; if ( x <= 20 ) x = 2*x; if ( x > 5 ) x++; 107. short is an integer data type that is two bytes long. char is also two bytes long but its values are interpreted as characters. The same bits are stored in both variables. What is displayed? short a = 0b0000000001000001; char b = 0b0000000001000001; System.out.println( a ); System.out.println( b ); You will not be tested on shorts or bytes. 8
if ( x > 5 ){ if ( x < 10 ) System.out.println("YES"); if ( x > 5 && x < 10 ) System.out.println("YES"); 108. Do the above two code snippets produce the same results? If they do not, what value of x will cause different results? if ( x > 5 ) System.out.println("NO"); if ( x < 2 ) System.out.println("NO"); System.out.println("MAYBE"); if ( x < 2 x > 5 ) System.out.println("NO"); System.out.println("MAYBE"); 109. Do the above two code snippets produce the same results? If they do not, what value of x will cause different results? if ( x > 20 ) { if ( x < 50 ) System.out.println("ONE"); System.out.println("TWO"); if ( x > 20 && x < 50 ) System.out.println("ONE"); System.out.println("TWO"); 110. Do the above two code snippets produce the same results? If they do not, what value of x will cause different results? 9