AP Comp Calculations
"catastrophe".substring(-1)
StringIndexOutofBoundsException (A StringIndexOutofBoundsException is thrown if the parameter of substring is less than zero. )
"catastrophe".substing(4,12)
StringIndexOutofBoundsException (A StringIndexOutofBoundsException is thrown since the end parameter of substring is greater than "catastrophe".length() which is 11. )
"catastrophe".substring(4,3)
StringIndexOutofBoundsException (A StringIndexOutofBoundsException is thrown since the end parameter of substring is less than the start parameter.)
"catastrophe".substring(12)
StringIndexOutofBoundsException (A StringIndexOutofBoundsException thrown if the parameter of substring is greater than "catastrophe".length(), namely, greater than 11 in this case.)
"catastrophe".substring(11)
"" (Note that 11 is "catastrophe".length(), in which case the method call returns the empty string "". )
"catastrophe".substring(4,5)
"s" (The method call "catastrophe".substring(4,5) returns the substring of "catastrophe" that starts at index 4 and goes to index 5 -1, namely, 4. Therefore the required substring has just one character, the "s" of "catastrophe".
"catastrophe".substring(4,9)
"strop" (The mthod call "catastrophe".substring(4,9) returns the substring of "catastrophe" that starts at index 4 and goes to index 9 -1, namely, 8. Therefore, the required substring starts at the "s"of "Catastrophe" and ends with "p".)
"catastrophe".substring(4)
"strophe" (The method call "catastrophe".substring(4) returns the substring of "catastrophe" that starts at index 4 and goes to the end of the string. Note that the "s" of "catastrophe" is at position 4 in the "catastrophe" string.)
"catastrophe".indexOf("a")
1 (The method call "catastrophe".indexOf("a") returns the index of the first occurance of "a" in "catastrophe", which is at position 1 in the string "catastrophe".)
51₈ as a binary number
101001₂ 51₈ = 5(8)¹ + 1(8)⁰ = 40 +1 = 41 Starting with the greatest power of 2 less than or equal to 41, express 41 as a sum of powers of 2 : 41 = 32 + 8 +1 1 (2)⁵ + 0(2)⁴ + 1(2)³ + 0(2)² + 0(2)¹ + 1(2)⁰ = 101001₂
2B₁₆ as a binary number
101011₂ (Recall that B represents 11 in a hexadecimal (base 16) number" 2B₁₆ = 2(16)¹ + 11(16)⁰ = 32 + 11 = 43 Starting with the greatest power of 2 less than or equal to 43, express 43 as a sum of powers of 2. 43 = 32 + 8 + 2 + 1 = 1(2)⁵ + 0(2)⁴ + 1(2)³ + 0(2)² + 1(2)¹ + 1(2)⁰ = 101011₂. )
1011₂ as a decimal number
11 1011₂ = 1(2)³+ 0(2)² + 1(2)¹ + 1(2)⁰ = 1 + 2 + 0 + 8 = 11
Integer.MAX_VALUE
2³²-1 (This is the mazimum int value, which is stored in 4 bytes or 32 bits. Since the first bit is used for the sign of the number, the maximum binary integer has a 1 in each of 31 slots, which equals 2³¹-1. )
The largest integre that can be stored in 1 byte ?
2⁷ -1 (The Picture below shows the largest positive integer that can be stored in 1 byte, which equals 8 bits. 01111111 The first bit is used for the sign of the number. Each of the other slots has the maximum binary number, namely, 1. The binary number 1111111₂ equals 2⁷-1. )
The number of bits used to store an int variable ?
32 (An int uses 4 bytes of storage, and each byte has 8 bits: 4X8 = 32 bits. )
The number of bytes used to store an int variable ?
4 (You just need to memorize this. )
2F₁₆ as a decimal number
47 Recall that in a hexadecimal (base 16) number, F represents 15: 2F₁₆ = 2(16)¹ + 15(16)⁰ = 32 + 16= 47
101₂ as a decimal number
5 101₂ = 1(2)²+ 0(2)¹ + 1(2)⁰ = 1+ 0 + 4 = 5
The number of bits in a byte ?
8 (You just need to memorize this. )
127₈ as a decimal number
87 127₈ = 1(8)² + 2(8)¹ + 7(8)⁰ = 64+16+7 = 87
"catastrophe".indexOf("mat")
-1 (The method call "catastrophe".indexOf("mat") returns the index of the first occurance of "mat" in "catastrophe".Since "mat" is not a substring of "catastrophe", the method returns -1.)
Integer.MIN_VALUE
-2³¹ (This is the minimum int value, which is stored in 4 bytes or 32 bits. Since the first bit is not used for the sign of the number (no need to store -0), the minimum binary integer has a 1 in each of 32 slots, which equals -2³¹.)
"catastrophe".indexOf("cat")
0 (The method call "catastrophe".indexOf("cat") returns the index of the first occurance of "cat" in "catastrophe". Note that the "c" of "cat" is at position 0 in the string "catastrophe". )