Programming Constructs - AP Computer Science A
Card 0 of 616
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
Compare your answer with the correct one above
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
Compare your answer with the correct one above
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
Compare your answer with the correct one above
Why is the "default" keyword used in a switch statement?
Why is the "default" keyword used in a switch statement?
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
Compare your answer with the correct one above
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
Compare your answer with the correct one above
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
Compare your answer with the correct one above
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
Compare your answer with the correct one above
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
int x=2;
double y=2.1;
float z=3.0;
int c=(x*y) + z;
What is the value of c
Remember that if a variable is declared as an integer, it can't have any decimals.
int c=(x*y) + z;
int c=(2*2.1)+3.0
From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.
int c=4.2+3
int c= 4+3
int c=7
Any leftover fractions are cut off, so the answer is shortened to just 7.
Remember that if a variable is declared as an integer, it can't have any decimals.
int c=(x*y) + z;
int c=(2*2.1)+3.0
From here, we do our math operations to solve, remembering to use the correct order of operations. Thus, start with the parentheses first then do the addition.
int c=4.2+3
int c= 4+3
int c=7
Any leftover fractions are cut off, so the answer is shortened to just 7.
Compare your answer with the correct one above
What does this factorial code snippet return after calling run()?
public void run() {
int n = 2;
n = factorial(n);
n = n + factorial(n);
System.out.println(n);
}
public int factorial(int n) {
if (n==1) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
What does this factorial code snippet return after calling run()?
public void run() {
int n = 2;
n = factorial(n);
n = n + factorial(n);
System.out.println(n);
}
public int factorial(int n) {
if (n==1) {
return 1;
}
else {
return n * factorial(n - 1);
}
}
4 will be printed out because factorial(2) = 2. Adding factorial(2) + 2 = 4. This can also be written as 2! + 2! = 4.
n is 2 when first assigned
n is then assigned to factorial(2) which is 2
n is then assigned n (which is 2) plus factorial(2) which is 2 so 2+2 = 4
n is 4 when it is printed out
4 will be printed out because factorial(2) = 2. Adding factorial(2) + 2 = 4. This can also be written as 2! + 2! = 4.
n is 2 when first assigned
n is then assigned to factorial(2) which is 2
n is then assigned n (which is 2) plus factorial(2) which is 2 so 2+2 = 4
n is 4 when it is printed out
Compare your answer with the correct one above
The Fibonacci sequence is a classic example of: _________
Choose the best answer.
The Fibonacci sequence is a classic example of: _________
Choose the best answer.
The Fibonacci sequence is known as a recurrence relation in mathematical terms. In programming, we represent recurrence relations using recursive methods or simply recursion. The Fibonacci sequence is a great example of recursion.
The Fibonacci sequence is known as a recurrence relation in mathematical terms. In programming, we represent recurrence relations using recursive methods or simply recursion. The Fibonacci sequence is a great example of recursion.
Compare your answer with the correct one above
public int foo(int n)
{
if (n < 0)
return 1;
else
return foo(n-2) + foo(n-1)
}
What is the value returned by foo(3)?
public int foo(int n)
{
if (n < 0)
return 1;
else
return foo(n-2) + foo(n-1)
}
What is the value returned by foo(3)?
The answer is 8.
This question tests knowledge of recursion. The correct strategy is to build a tree of function calls. Since the base case of foo returns 1, we can just add up all the function calls that return the base case. In the tree below, the green functions represent the base cases, with an argument less than 0.

The answer is 8.
This question tests knowledge of recursion. The correct strategy is to build a tree of function calls. Since the base case of foo returns 1, we can just add up all the function calls that return the base case. In the tree below, the green functions represent the base cases, with an argument less than 0.
Compare your answer with the correct one above
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
True or False.
The output of this code snippet will be "Hello, I'm hungry!"
public static void meHungry() {
String hungry = "hungry";
String iAm = "I'm";
String hello = "Hello";
String message = "";
if (hungry != null) {
message += hungry;
}
if (hello != null && iAm != null) {
message = hello + iAm + hungry;
}
System.out.println(message);
}
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
The message that is printed out is "Hello I'm hungry"
Notice there is no punctuation in the message. The code does not add punctuation to the message, but prints the words out in the same order as the phrase in the prompt. Be mindful of what's actually happening in the code.
Compare your answer with the correct one above
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
In Swift (iOS), give a description of what this method does.
func getDivisors(num: Int, divisor: Int) -> Bool {
var result: Bool = False
if (num % divisor == 0) {
result = True
}
println(result)
}
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
The modulus function "%" determines the remainder of a division. If I have 1 % 2, the remainder is 1. If I have 2 % 2, the remainder is 0. Therefore, if the remainder is equal to 0, then the number is divisible by the function. Thus, the method returns true if the number is evenly divisble by the divisor and false otherwise.
Compare your answer with the correct one above
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
Does the code compile and if yes, what is the the output?
public class Test
{
public static void main ( String \[\] args )
{
int x = 2;
if (x = 2)
System.out.println("Good job.");
else
System.out.println("Bad job);
}
}
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
It doesn't compile. It is supposed to be x == 2 within the if statement not x = 2. The if statement checks for booleans (only true or false) and cannot convert an integer into a boolean to meet that check.
Compare your answer with the correct one above
Why is the "default" keyword used in a switch statement?
Why is the "default" keyword used in a switch statement?
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
The default keyword is used to catch any remaining cases not specified. For example, if you only want to check for certain hour of the day, say 2,3, and 4 o'clock, the default case would catch all the other hours. You could then create a statement for what to do when those values are caught.
Compare your answer with the correct one above
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
True or False.
This code snippet will iterate 5 times.
ArrayList<String> arrList = new ArrayList<String>();
arrList.add("string0");
arrList.add("string1");
arrList.add("string2");
arrList.add("string3");
arrList.add("string4");
for (int i = 0; i < arrList.size(); i++) {
System.out.println(arrList.get(i));
}
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
The ArrayList is populated with 5 strings. The for loop will iterate through the ArrayList from position 0 to 4 because once i gets to 5 the loop with exit. 0 to 4 is five iterations. So the answer is true.
Compare your answer with the correct one above
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
Suppose you have the following code:
public static void main(String\[\] args) {
int a =2;
if (a%2==0)
System.out.println("Hello World");
else
System.out.println("Hi");
}
If the main method is called, what will be printed?
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
"Hello World" will be printed, since the first condition is true: 2%2=0, or equivalently 2 is an even number. Once a condition in an if block is executed, the if block is exited. This means that any other elseif or else clauses will not be executed. If a%2==0 were False, then "Hi" would be printed. In no situation would it be possible for both "Hello World" and "Hi" to be printed. Additionally, no errors would be thrown since the syntax is correct and no runtime errors occur.
Compare your answer with the correct one above
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
public void draw() {
recurs(11);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What does the code print?
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
This creates an infinite loop because the condition to end the loop is never reached. Since count is never equal to 0, count continues to be entered into recurs over and over with no end.
Compare your answer with the correct one above
// ^ is Exclusive-Or (XOR) operator
x = x ^ y
y = x ^ y
x = x ^ y
What is the effect of the above snippet of code?
// ^ is Exclusive-Or (XOR) operator
x = x ^ y
y = x ^ y
x = x ^ y
What is the effect of the above snippet of code?
Consider two variables:
(binary 1010) and
(binary 0101). The exclusive or operator returns 1 in every bit that is different in the two numbers and returns 0 in every bit that is the same.
In order to switch the numbers, we must do this:
^ 
^ 
^ 
Continuing with the example,
^
would return 1111, because no bits are the same between the two numbers. Store this in the variable
. Then 1111 ^ 0101 would return 1010. That result will be stored in
. Finally, find the value of
(1111) ^
(1010) which is 0101, and store the result in variable
. The values are now switched.
Consider two variables: (binary 1010) and
(binary 0101). The exclusive or operator returns 1 in every bit that is different in the two numbers and returns 0 in every bit that is the same.
In order to switch the numbers, we must do this:
^
^
^
Continuing with the example, ^
would return 1111, because no bits are the same between the two numbers. Store this in the variable
. Then 1111 ^ 0101 would return 1010. That result will be stored in
. Finally, find the value of
(1111) ^
(1010) which is 0101, and store the result in variable
. The values are now switched.
Compare your answer with the correct one above
What are the 3 different manners (in terms of accessibilitiy) in which you can declare a variable and what are the differences between the 3?
What are the 3 different manners (in terms of accessibilitiy) in which you can declare a variable and what are the differences between the 3?
Public is accessible to everyone regardless if the two classes are in the same package or not.
Protected: Only allows access of those extended are packaging.
Private: Its meant to keep private in order for only members of that specific class to see it.
Public is accessible to everyone regardless if the two classes are in the same package or not.
Protected: Only allows access of those extended are packaging.
Private: Its meant to keep private in order for only members of that specific class to see it.
Compare your answer with the correct one above