APCOMSCI exam
Which of the following is true after the code executes? (2 points) String anim1 = "dog"; String anim2 = "dog"; String anim3 = "cat";
!anim1.equals(anim3)&& anim1.equals(anim2)
Int iNum = 55/100
0
What output will be produced by the following code segment? (2 points) int num = 0; while(num < 20) { System.out.print(num + " "); num += 4; } System.out.println(num);
0 4 8 12 16 20
What is the range of possible values for the variable x? (2 points) int x = (int)(Math.random() * 10);
0 <= x <= 9
Double dNum = 45/100
0.0
12 < 12.0 12 != 12.0 12 == 12.0
1 & 2
Which of the following statements contains an error? (3 points) double heightCm = 2.54 / heightIn; if(true) double bMR = (10.0 * weightKg) + (6.25 * heightCm) - (5.0 * age) + s;
1 & 2
Which of these expressions evaluates to 4.5? 9 / (double)2 (double)9 / 2 (double)(9 / 2)
1 & 2
Which of the following statements does NOT contain an error? (2 points) String firstInitial = firstName.substring(0,1); String lastName = in.Next(); String name = firstInitial + ". " + lastName;
1 & 3
What is the result of the following code segment? (3 points) int x = 10; if(x + 1 < 20) x += 5; System.out.println(x);
15
Given the code below. String line = "This is a String."; What is the value of line.length()? (2 points)
17
String str = "Lord of the Rings";, then what is the value of str.length();?
17
What does the binary number 10011 represent in the decimal system?
19
If String str = "Always pass on what you have learned.";, what is the value of str.indexOf("way");?
2
Int iNum = 14/5
2
Which of the following expressions will evaluate to true? (3 points) "Samira".equals("Sami") "SAMI".compareTo("Samira") < 0 "Sami".compareTo("SAMI") < 0
2 only
int randomNum = rand.nextInt(5) +1; Which set or sets of integers represent possible values for randomNum? (3 points) 0, 1, 2, 3, 4, 5 1, 2, 3, 4, 5 1, 2, 3, 4, 5, 6
2 only
Consider the following code segment. int x = 9; int y = 3; double z = 2.0; System.out.println(4 + x / y * z − 1);
9.0
What is the return type of the String class equals() method
Boolean
String str = "Computer Science";, then what is the value of str.substring(4, 6);?
Ut
if (a != b) { ** code block ** if (b > c) { ** code block ** } }
if (a!=b) && (b›c)
If String str = "United States";, then what is the value of str.substring(3);? (2 points)
ted States
Consider the code below. Which of the lines of code can replace the code block to result in 16 being printed for count? (2 points) int count = 1; ** code block ** { count *= 2; } System.out.println(count);)
while (count <= 8)
Which of the following expressions is equivalent to !(c > d)? (2 points)
(c < = d)
In the Guess My Number game, there is a lower limit and an upper limit for the range of possible numbers to guess. If the lower limit were inclusive and the upper limit exclusive, which expression would properly generate values for the secret number? (4 points)
(int)(Math.random() * (upper − lower) ) + lower
If String str = "Always pass on what you have learned.";, what is the value of str.indexOf("always");?
-1
String str = "United States";, then what is the value of str.indexOf("united");?
-1
What is the value of num when the code below executes? (2 points) String msg = "Java a great part of my day!"; int num = msg.indexOf("Great");
-1
Consider the code block below. What is the value of the variable total when this method is called twice, the first time using addTotal(5) and then using addTotal(−3)? (2 points) public static int addTotal(int itemCost) { int total = 0; total += itemCost; return total; }
-3
Double dNum = 55/100.0
0.55
How can the constructor be identified in a class file? (3 points) The name of the constructor is the same as the name of the class. The constructor's parameter list will always be empty. The constructor is like a method, but has a void return type.
1
String greet2 = "Good Afternoon!"; int result; if (greet1.compareTo(greet2) > 0) result = 1; else if (greet1.compareTo(greet2) < 0) result = −1; else result = 0; What is the value of result after it runs? (2 points)
1
Given the following code segment, which of the expressions could take the place of < condition > to cause 10 iterations of the while loop? (2 points) int flipCount = 0; while( ‹condition› ) { // code not shown flipCount++; } flipCount < 10 flipCount <= 9 flipCount == 10
1 & 2
Which of following expressions will evaluate to false? 11 / 7 > 1.0 11 / 7.0 == 1.0 (int)(11.0 / 7.0) == 1.0
1 & 2
Which of the following statements contains an error? (3 points) char gender = genderString.indexOf('0'); boolean isFemale = gender == "F"; int age = Integer.parseInt(ageString);
1 & 2
Errors can be syntax errors or logic errors (the code works, but not as intended). Which of the following statements contains an error? (3 points) String genderString = in.nextChar(); String ageString = in.next(); String heightInchesString = in.nextString()
1 & 3
What is printed as a result of the code block below? (2 points) for (int m = 0; m < 4; m+=1) { int n = 0; while(n < 4) { if (n%2==1) n+=2; n++; System.out.print(n + " "); } }
1 4 1 4 1 4 1 4
What conclusion can be made about the state of the program when the while loop terminates? Assume answer is a declared and initialized String. (4 points) while(!answer.equals( "N")) { // code not shown }
1) The value of answer is N
How many times will the following while loop repeat? (2 points) int flipCount = 0; while(flipCount < 10) { // code not shown flipCount++; }
10
int count = 1; for(int outer = 0; outer < 3; outer++) for(int inner = outer; inner < 4; inner++) count++;
10
int number = 6 + 19 % 5;
10
What does the decimal number 257 equal in the hexadecimal system?
101
What does the hexadecimal number 2D represent in the binary system?
101101
String str = "I ate chocolate."; String newStr = str.replace("ate", "8"); System.out.println(newStr.length());
12
Double dNum = (int)(2.5*5)
12.0
int x = 1; int y = 0; while (x < 10) { y = 5; while (y > x) { y--; } x += y; } System.out.println(x);
13
Compare the user's guess to the lottery number and determine if the back pairs match. Which of the following statements contains an error? (4 points) else if( userBackPair.equals(lotteryBackPair) ) else if( !userFrontPair.equals(lotteryFrontPair) ) else if( userPick == lotteryNum )
2 & 3 (u cant use == with strings)
for (int j = 0; j < 16; j++) { if (j % 3 == 2) System.out.print(j + " "); } What is printed when the code block is executed? (2 points)
2 5 8 11 14
Errors can be syntax errors or logic errors (the code works, but not as intended). Which of the following statements contains an error? (3 points) import java.util.Scanner; public static void Main(String [] args) Scanner in = new Scanner(System.in);
2 only
Telephone numbers are in the form of 1234567890. The first 3 digits, "123," are considered the area code. Consider the code below: String phoneNum1 = //contains any valid phone number, implementation not shown Which of the following statements correctly obtains the area code of the number? (4 points) String areaCode1 = phoneNum1.substring(0, 2); String areaCode1 = phoneNum1.substring(0, 3); String areaCode1 = phoneNum1.substring(3);
2 only
What is the result of double num = 10/4?
2.0
Double dNum = 2.02
2.02
What does the hexadecimal number EF represent in the decimal system?
239
What does the octal number 362 represent in the decimal system?
242
String word = "Programming"; System.out.println(word.indexOf('g'));
3
What is the result of the following code segment? (3 points) String word = "starfish"; String x = word.substring(4); String y = word.substring(3, 8); if( x == y) System.out.print(1); else if( x.equals(y)) System.out.print(2); else System.out.print(3);
3
Which of the following statements contains an error? (2 points) Scanner in = new Scanner(System.in); System.out.print("Please enter your name (first last): "); String firstName = in.Next();
3 only
Which of the following statements contains an error? (2 points) String firstName = in.next(); String lastName = in.nextLine(); System.out.print("\n");
3 only
Which of the following statements contains an error? (2 points) System.out.print("Please enter your age in years: "); String userAge = in.next(); int ageInYears = integer.ParseInt(userAge);
3 only
Which of the following statements does NOT contain an error? (2 points) System.out.print("/tab Age in Minutes: " + ageInMinutes + "/n"); System.out.print("/t Age in Minutes: " + ageInMinutes + "/n"); System.out.print("\t Age in Minutes: " + ageInMinutes + "\n");
3 only
Double dNum = (double) (10/3)
3.0
Double dNum = 10/3
3.0
Double dNum = (double)10/3
3.333333
Int iNum = (int)38.78
38
Double dNum = (int)38.78
38.0
Using the following code segment, which best describes the output? (3 points) for(int outer = 0; outer < 5; outer += 2) { for(int inner = 0; inner < outer + 1; inner++) { System.out.print("*"); } System.out.println(); }
4) A triangle resting on its base of 5 stars
What will be written to the screen when the following code runs? int num = 12; num −= 7; System.out.println(num);
5
Double dNum = (int)2.5*3
6.0
(20 + 3) / 3
7
Int iNum = 7
7
What is the result of 67 % 10?
7
Double dNum = 7
7.0
String str = "Florida Keys";, then what is the value of str.indexOf("eys");? (2 points)
9
Assume the String objects x and y are initialized as shown below. What is the value of the expression word1.compareTo(word2)? (3 points) String word1 = "jumping"; String word2 = "jack";
A positive integer
System.out.println("APCS \nis the best!");
APCS is the best!
Assume an integer variable named num is assigned a value of 15. What is the value of num + 6 < 20? (3 points)
False
Assume the String objects word1 and word2 are initialized as shown below. What is the value of the expression word1.equals(word2)? (3 points) String word1 = "Earth"; String word2 = "earth";
False
String str = "I ate chocolate."; String newStr = str.replace("ate", "8"); System.out.println(newStr.length()); If the user were to enter "Field of Dreams" as the book title what would be printed as a result on line 3
Field
If String str = "houseboat";, which of the following code segments will return the word boat? (2 points) str.substring(4, 9); str.substring(5, 9); str.substring(5);
II and III only
Assume that the Scanner class has already been imported. Consider the code block below. Scanner input = new Scanner (System.in); System.out.println("How old are you?"); String age = input.next(); int newAge = Integer.parse(age); Which, if any, lines have errors on them? (2 points)
IV
(3 + 4 / (int)3.14) * 5
Int
Which statement is true about a constructor?
It is a method used when instantiating an object
Assume the integer variable num has been assigned a valid value. What is the purpose of the following code segment? (3 points) if(num % 10 != 0) System.out.print(num);
It prints num if its value is not a multiple of 10.
The loop must iterate 3 times to generate a lottery number. Which of the following loops contains an error? (4 points) for(int n = 1; n < lotNumLength; n++) for(int n = 0; n <= lotNumLength; n++) for(int n = 0; n < lotNumLength; n--)
Line 1 2 3
The intent of the following code segment is to simulate flipping a coin five times. What happens when the code is run? (2 points) int flipCount = 1; while(flipCount < 5) { //code not shown flipCount++; }
The loop iterates one too few times.
String name = "Shakespeare"; int x = name.indexOf("s") / 2; String newStr = name.substring(x, x + 3); System.out.println(newStr);
ake
Consider the code block below. What is printed as a result of newString("coding")? (2 points) public static void newString(String word) { String newStr = ""; for (int x = 0; x < word.length(); x+=2) { newStr += word.substring(x); } System.out.println(newStr); }
codingdingng
Within a class __________, the class __________ is executed each time an instance of the class is created.
definition, body
A programmer needs to simulate five trials. During each trial, two dice are rolled 100 times. Which loop structure best represents the code needed to accomplish this task? (3 points)
for(int t = 0; t < 5; t++) for(int r = 0; r < 100; r++)
String msg = "Today is a great day for programming!"; String newMsg = msg.substring(msg.indexOf('g')); System.out.println(newMsg);
great day for programming!
If String str1 = "hello"; and String str2 = "world";, then what is the value of str1 + str2?
helloworld
String phoneNum1 = //contains any valid phone number, implementation not shown String phoneNum2 = //contains any valid phone number, implementation not shown Which of the following blocks of code determines that the numbers are equal numerically? (4 points) Example: String phoneNum1 = "407-555-2207"; String phoneNum2 = "407-555-2207";
if(phoneNum1.compareTo(phoneNum2) == 0) { //phoneNum1 and phoneNum2 are equal }
public class acceptInput { public static void main (String[ ] args) { Scanner scan = new Scanner (System.in); System.out.println("How old are you? "); int age = scan.nextInt(); System.out.println("Wow you are " + age + " years old. "); } }
import java.util.Scanner; is missing
Which of the following will correctly generate a random digit from 1 through 10? (4 points)
int digit = (int) (Math.random()*10) + 1;
A large quantity of purple and yellow stones are placed in a bag. There is a 40 percent chance of drawing a purple stone. Write the section of code to simulate randomly picking 20 stones from the bag and reporting the color of each. (5 points)
int stonesPicked = 0; int purple = 0; int yellow = 0; double randNum = 0.0; while(stonesPicked < 20) { randNum = Math.random(); if(randNum < 0.4) { purple++; System.out.println("Stone: purple"); } else { yellow++; System.out.println("Stone: yellow"); } stonesPicked++; }
String name = "Voltaire"; String newStr = name.substring(2,5); System.out.println(newStr);
lta
Complete the code below that would store "day" to the variable newString. (3 points) String oldString = "Happy Friday Programmers!"; String newString = ________________________;
oldString.substring(9,12);
The method header is defined as one or more modifiers, followed by the ________ type and then the method __________.
return, signature
Which is a method signature? (2 points)
someMethod(int num, String s)