AP COMPUTER SCIENCE A: UNIT 2 EXAM
Consider the following variable declarations.
int a = 5;
int b = 4;
double x = 5;
double y = 4;
Which of the following expressions would evaluate to the number, 1?
1. (int) x / y
2. a / b
3. a % b - Answer -All of them. (1 , 2, and 3)
Consider the following variable declarations.
1. double x = (int) 3.4
2. int y = (int) , 5
3. String z = new String(" ") - Answer -1 and 3
Consider String a below. Which of the following would create a new String with the
value, "ram"?
String a = new String ("Java Program"); - Answer -String b = a.substring(9);
Better code will NOT type in a static number to produce words near the end of the
String and instead, uses length. Which of the following will result in "ram" in a better way
than #2 did? - Answer -String b = a.substring(a.length()-3);
What special type of data is identified it has methods , unlike primitive data? Another
giveaway that this is not primitive data is that variables of this type begin with a capital
letter. - Answer -Objects
Consider the code segment below. What is the result that is printed on the screen?
String six = new String ("Question 6");
int find = six.indexOf("q");
find = find + 2;
System.out.println(find + six.indexOf("t"); - Answer -5
What is the value stored in z by the end of the code segment?
int x = 3;
int y = 6;
double z = x + y / 2; - Answer -6.0
What is the value stored in c by the end of the code segment?
Consider the following variable declarations.
int a = 5;
int b = 4;
double x = 5;
double y = 4;
Which of the following expressions would evaluate to the number, 1?
1. (int) x / y
2. a / b
3. a % b - Answer -All of them. (1 , 2, and 3)
Consider the following variable declarations.
1. double x = (int) 3.4
2. int y = (int) , 5
3. String z = new String(" ") - Answer -1 and 3
Consider String a below. Which of the following would create a new String with the
value, "ram"?
String a = new String ("Java Program"); - Answer -String b = a.substring(9);
Better code will NOT type in a static number to produce words near the end of the
String and instead, uses length. Which of the following will result in "ram" in a better way
than #2 did? - Answer -String b = a.substring(a.length()-3);
What special type of data is identified it has methods , unlike primitive data? Another
giveaway that this is not primitive data is that variables of this type begin with a capital
letter. - Answer -Objects
Consider the code segment below. What is the result that is printed on the screen?
String six = new String ("Question 6");
int find = six.indexOf("q");
find = find + 2;
System.out.println(find + six.indexOf("t"); - Answer -5
What is the value stored in z by the end of the code segment?
int x = 3;
int y = 6;
double z = x + y / 2; - Answer -6.0
What is the value stored in c by the end of the code segment?