Program Implementation - AP Computer Science A
Card 0 of 693
int var=0;
int array\[4\];
for (int j=0; j<=4;j++)
{
var=var+1;
}
What is the value of var?
int var=0;
int array\[4\];
for (int j=0; j<=4;j++)
{
var=var+1;
}
What is the value of var?
The for loop simply sums up the numbers 0 through 3, which is 6.
First iteration through the loop, j=0:
var=0+1=1
Second iteration, j=1:
var=1+1=2
Third iteration, j=2:
var=2+1=3
Fourth iteration, j=3:
var=3+1=4
Fifth iteration, j=4:
var=4+1=5
The for loop simply sums up the numbers 0 through 3, which is 6.
First iteration through the loop, j=0:
var=0+1=1
Second iteration, j=1:
var=1+1=2
Third iteration, j=2:
var=2+1=3
Fourth iteration, j=3:
var=3+1=4
Fifth iteration, j=4:
var=4+1=5
Compare your answer with the correct one above
#include
using namespace std;
int main()
{
int i=0;
int sum =0;
for(i;i<4;i+=2)
{
sum=sum+i;
i--;
}
return 0;
}
What is the value of i and sum?
#include
using namespace std;
int main()
{
int i=0;
int sum =0;
for(i;i<4;i+=2)
{
sum=sum+i;
i--;
}
return 0;
}
What is the value of i and sum?
Let's take a look at the declaration of the foor loop. It will run as long at the integer "i" is less than four. It also increases by 2 after each iteration. i+=2 is equivalent to i=i+2.
Now let's look at the contents of the loop itself. The integer "sum" starts at 0 and is added to i at each iteration. After this we see that 1 number is subtracted from i after each iteration. Now after examining the structure of the loop, let's begin to calculate each iteration.
Iteration 1: i=0, sum=0, then i=-1.
Iteration 2: i=1, sum=1, then i=0.
Iteration3: i=2, sum=3, then i=1,
Iteration 4: i=3, sum =6, then i=3.
There is no iteration 5 because "i" can only be less than four to enter the loop, so the program ends there.
i=3, sum=6
Let's take a look at the declaration of the foor loop. It will run as long at the integer "i" is less than four. It also increases by 2 after each iteration. i+=2 is equivalent to i=i+2.
Now let's look at the contents of the loop itself. The integer "sum" starts at 0 and is added to i at each iteration. After this we see that 1 number is subtracted from i after each iteration. Now after examining the structure of the loop, let's begin to calculate each iteration.
Iteration 1: i=0, sum=0, then i=-1.
Iteration 2: i=1, sum=1, then i=0.
Iteration3: i=2, sum=3, then i=1,
Iteration 4: i=3, sum =6, then i=3.
There is no iteration 5 because "i" can only be less than four to enter the loop, so the program ends there.
i=3, sum=6
Compare your answer with the correct one above
public static boolean remove(int[] arr, int val) {
boolean found = false;
int i;
for(i = 0; i < arr.length && !found; i++) {
if(arr[i] == val) {
found = true;
}
}
// START
if(found) {
for(int j = i; j < arr.length;j++) {
arr[j - 1] = arr[j];
}
arr[arr.length - 1] = 0;
}
// END
return found;
}
What does the code between // START and // END do?
public static boolean remove(int[] arr, int val) {
boolean found = false;
int i;
for(i = 0; i < arr.length && !found; i++) {
if(arr[i] == val) {
found = true;
}
}
// START
if(found) {
for(int j = i; j < arr.length;j++) {
arr[j - 1] = arr[j];
}
arr[arr.length - 1] = 0;
}
// END
return found;
}
What does the code between // START and // END do?
The easiest way to approach this question is to comment on this line by line. Notice that the condition only works if the given value was found. (This is the use of the boolean found.) If the desired value has indeed been located, the code then goes through the array, picking up just past the location of the index. (At this point, i is one past the index of the value to be deleted.) Then, it goes through the code and moves backward the value at each index. In effect, this shifts the content backward, giving the appearance of having performed a deletion. This is done on the line arr[j - 1] = arr[j];. Finally, at the end, the final index value is set to a default value, namely 0. (This is done on the line arr[arr.length - 1] = 0;.)
The easiest way to approach this question is to comment on this line by line. Notice that the condition only works if the given value was found. (This is the use of the boolean found.) If the desired value has indeed been located, the code then goes through the array, picking up just past the location of the index. (At this point, i is one past the index of the value to be deleted.) Then, it goes through the code and moves backward the value at each index. In effect, this shifts the content backward, giving the appearance of having performed a deletion. This is done on the line arr[j - 1] = arr[j];. Finally, at the end, the final index value is set to a default value, namely 0. (This is done on the line arr[arr.length - 1] = 0;.)
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
Suppose you are given the following lines of code (and x is some initialized integer):
int k = arr.length;
for (int i = -1; i <k-2; i++)
{
if (arr\[i+2\] < x)
System.out.print("Hello");
}
What is the maximum number of times "Hello" print?
Suppose you are given the following lines of code (and x is some initialized integer):
int k = arr.length;
for (int i = -1; i <k-2; i++)
{
if (arr\[i+2\] < x)
System.out.print("Hello");
}
What is the maximum number of times "Hello" print?
The answer is k-1, where k is the length of the array.
This problem tests the students' understanding of loop itterations. The loop counter begins at -1, but the first element of arr to be accessed is at index=1. Likewise, the loop exits when i<k-2 is no longer true, and the final index of arr to be accessed is at index=k-3+2=k-1, which is the last element of arr. Therefore, we do not get an ArrayIndexOutOfBoundsException because the array is being accessed properly.
Since the (k-1)th element of arr is accessed (the last element) and the 0th element of arr is skipped (the first element to be accessed is at index=1, and arrays are 0 indexed), there are k-1 loop itterations. If all the values in arr do not exceed x, then "Hello" will printed k-1 times.
The answer is k-1, where k is the length of the array.
This problem tests the students' understanding of loop itterations. The loop counter begins at -1, but the first element of arr to be accessed is at index=1. Likewise, the loop exits when i<k-2 is no longer true, and the final index of arr to be accessed is at index=k-3+2=k-1, which is the last element of arr. Therefore, we do not get an ArrayIndexOutOfBoundsException because the array is being accessed properly.
Since the (k-1)th element of arr is accessed (the last element) and the 0th element of arr is skipped (the first element to be accessed is at index=1, and arrays are 0 indexed), there are k-1 loop itterations. If all the values in arr do not exceed x, then "Hello" will printed k-1 times.
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
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 lairotcaf (int n) {
if (n <= 1){
return 1
}
temp = n * lairotcaf(n-1)
return temp;
}
int num;
cin >> num;
``
if (num >= 0){
lairotcaf(n)
} else {
cout << "must use number greater or equal to zero"< endl;
}
What is returned by lairotcaf(5)?
int lairotcaf (int n) {
if (n <= 1){
return 1
}
temp = n * lairotcaf(n-1)
return temp;
}
int num;
cin >> num;
``
if (num >= 0){
lairotcaf(n)
} else {
cout << "must use number greater or equal to zero"< endl;
}
What is returned by lairotcaf(5)?
In recursion, a value is calculated in reverse until a point at which an answer is defined. From that point, the definition is used to calculate forward, evaluating the other definitions which rely upon that base condition.
Recursive functions must have a test that will halt recursion. If the current state of the recursive function matches the base condition, the recursion should stop. Otherwise, recursion should continue.
In case you did not notice from the backwards-naming scheme, this is a definition for the factorial function. The function will not be called if a negative number is passed. If 0 or 1 is passed, 1 is returned. 0 or 1 is the base condition. If the number is greater than one, a chain of calls to lairotcaf() will initiate until a base condition is reached.
Once lairotcaf(5) is expanded, we have:

In recursion, a value is calculated in reverse until a point at which an answer is defined. From that point, the definition is used to calculate forward, evaluating the other definitions which rely upon that base condition.
Recursive functions must have a test that will halt recursion. If the current state of the recursive function matches the base condition, the recursion should stop. Otherwise, recursion should continue.
In case you did not notice from the backwards-naming scheme, this is a definition for the factorial function. The function will not be called if a negative number is passed. If 0 or 1 is passed, 1 is returned. 0 or 1 is the base condition. If the number is greater than one, a chain of calls to lairotcaf() will initiate until a base condition is reached.
Once lairotcaf(5) is expanded, we have:
Compare your answer with the correct one above
Consider the following code:
public static void main(String\[\] args) {
System.out.println(equation(8));
}
public static int equation(int a) {
if(a <= 5) {
return 12;
}
return equation(a-2) * equation(a-1);
}
What is the output for the code above?
Consider the following code:
public static void main(String\[\] args) {
System.out.println(equation(8));
}
public static int equation(int a) {
if(a <= 5) {
return 12;
}
return equation(a-2) * equation(a-1);
}
What is the output for the code above?
The function equation is a recursive method, meaning that it calls itself. The easiest way to trace a recursive method is to first pay heed to its stopping case. For this, it is:
if(a <= 5) {
return 12;
}
In this case, it returns 12 for any values less that or equal to 5. Now, begin your trace:
equation(8) = equation(6) * equation(7)
equation(7) = equation(5) * equation(6)
equation(6) = equation(4) * equation(5)
Therefore, equation(6) is:
12 * 12 or 144
Thus, we know:
equation(7) = 12 * 144 = 1728
And, equation(8) = 144 * 1728 = 248832
The function equation is a recursive method, meaning that it calls itself. The easiest way to trace a recursive method is to first pay heed to its stopping case. For this, it is:
if(a <= 5) {
return 12;
}
In this case, it returns 12 for any values less that or equal to 5. Now, begin your trace:
equation(8) = equation(6) * equation(7)
equation(7) = equation(5) * equation(6)
equation(6) = equation(4) * equation(5)
Therefore, equation(6) is:
12 * 12 or 144
Thus, we know:
equation(7) = 12 * 144 = 1728
And, equation(8) = 144 * 1728 = 248832
Compare your answer with the correct one above
Which of the following is a recursive factorial function?
Recall that an example of a factorial is:

Which of the following is a recursive factorial function?
Recall that an example of a factorial is:
Recall that a recursive function calls itself. Therefore, you can eliminate several of the answers just by the fact that they are not recursive in nature. (Indeed the one with a loop is a correct computation.) Now, you can think of a factorial like this:

And then...

And so forth.
Thus, you have a stopping case at 1, which is equal to 1. For any other value, you will return a recursive value, namely the parameter a and then the factorial method called with a-1.
Recall that a recursive function calls itself. Therefore, you can eliminate several of the answers just by the fact that they are not recursive in nature. (Indeed the one with a loop is a correct computation.) Now, you can think of a factorial like this:
And then...
And so forth.
Thus, you have a stopping case at 1, which is equal to 1. For any other value, you will return a recursive value, namely the parameter a and then the factorial method called with a-1.
Compare your answer with the correct one above
Consider the following code:
public static void main(String[] args) {
System.out.println(foo("This is my favorite: Yay for programming!!!"));
}
``
public static String foo(String s) {
if(!s.equals("")) {
char c = s.charAt(0);
if(c >= 'A' && c <= 'Z') {
return Character.toLowerCase(c) + foo(s.substring(1));
} else if (c >= 'a' && c <= 'z'){
return Character.toUpperCase(c) + foo(s.substring(1));
}
return foo(s.substring(1));
}
return "";
}
What is the output for the main function above?
Consider the following code:
public static void main(String[] args) {
System.out.println(foo("This is my favorite: Yay for programming!!!"));
}
``
public static String foo(String s) {
if(!s.equals("")) {
char c = s.charAt(0);
if(c >= 'A' && c <= 'Z') {
return Character.toLowerCase(c) + foo(s.substring(1));
} else if (c >= 'a' && c <= 'z'){
return Character.toUpperCase(c) + foo(s.substring(1));
}
return foo(s.substring(1));
}
return "";
}
What is the output for the main function above?
The foo method is clearly recursive. Therefore, let's look at our conditions:
If s is "", then the code reaches the base case (at the very end): return "";
Now, for other strings, there are two cases embedded:
c >= 'A' && c <= 'Z' and c >= 'a' && c <= 'z'
Notice that c is the first character of the string. So, for these two cases, if the first character is upper case, the function makes it lower, and if it is lower, it makes it upper case. Then, it makes a recursive call to itself, using the remainder of the string after this first character. If neither of those are reached, it just does a call to itself, ignoring the first character. Therefore, this code will "flip" any cases of alphabetic characters but will ignore all other characters. That is why the correct answer is the compressed-looking string: tHISISMYFAVORITEyAYFORPROGRAMMING
The foo method is clearly recursive. Therefore, let's look at our conditions:
If s is "", then the code reaches the base case (at the very end): return "";
Now, for other strings, there are two cases embedded:
c >= 'A' && c <= 'Z' and c >= 'a' && c <= 'z'
Notice that c is the first character of the string. So, for these two cases, if the first character is upper case, the function makes it lower, and if it is lower, it makes it upper case. Then, it makes a recursive call to itself, using the remainder of the string after this first character. If neither of those are reached, it just does a call to itself, ignoring the first character. Therefore, this code will "flip" any cases of alphabetic characters but will ignore all other characters. That is why the correct answer is the compressed-looking string: tHISISMYFAVORITEyAYFORPROGRAMMING
Compare your answer with the correct one above
Which of the following is a recursive method for summing the contents of a list of integers? (Choose the best answer.)
Which of the following is a recursive method for summing the contents of a list of integers? (Choose the best answer.)
Recall that a recursive method is one that calls itself, slowly working toward a "base case" at which it terminates. Therefore, you can immediately eliminate the option that has an iterative loop. This is not recursion.
Next, however, there are two methods that have return values:
return ints.get(0) + val(ints.subList(1, ints.size()));
return ints.get(0) + val(ints - 1);
Neither of these will work, for they never terminate. (Well, they terminate, but only with an error!) Also, the second is nonsensical anyway—you cannot subtract 1 from a list as a whole!
Finally, consider the option with a recursive return value of:
return ints.remove(0) + val(ints);
This actually will add the items correctly. However, by removing items from the list, you will thus destroy the list in the course of adding them! Therefore, the remaining option is the best one for this question:
public static int val(List ints) {
if(ints.size() == 0) {
return 0;
} else {
return ints.get(0) + val(ints.subList(1, ints.size()));
}
}
Recall that a recursive method is one that calls itself, slowly working toward a "base case" at which it terminates. Therefore, you can immediately eliminate the option that has an iterative loop. This is not recursion.
Next, however, there are two methods that have return values:
return ints.get(0) + val(ints.subList(1, ints.size()));return ints.get(0) + val(ints - 1);
Neither of these will work, for they never terminate. (Well, they terminate, but only with an error!) Also, the second is nonsensical anyway—you cannot subtract 1 from a list as a whole!
Finally, consider the option with a recursive return value of:
return ints.remove(0) + val(ints);
This actually will add the items correctly. However, by removing items from the list, you will thus destroy the list in the course of adding them! Therefore, the remaining option is the best one for this question:
public static int val(List ints) {
if(ints.size() == 0) {
return 0;
} else {
return ints.get(0) + val(ints.subList(1, ints.size()));
}
}
Compare your answer with the correct one above
public static int foo(int a, int b) {
if(b <= 1 || b <= a) {
return 1;
}
return (b - a) * foo(a,b-1);
}
Based on the code above, what is the value of the following function call:
foo(5,9);
public static int foo(int a, int b) {
if(b <= 1 || b <= a) {
return 1;
}
return (b - a) * foo(a,b-1);
}
Based on the code above, what is the value of the following function call:
foo(5,9);
The function foo is recursive, meaning that it calls itself. Therefore, you should start by looking for its termination point. Based on the definition above, it terminates when either b <= 1 or b <= a. In these two cases, it will return 1.
Now, let us do our trace of the calls:
foo(5,9) = (9-5) * foo(5,8) = 4 * foo(5,8)
foo(5,8) = (8-5) * foo(5,7) = 3 * foo(5,7)
foo(5,7) = (7-5) * foo(5,6) = 2 * foo(5,6)
foo(5,6) = (6-5) * foo(5,5) = 1 * foo(5,5)
foo(5,5) = 1
Therefore, carefully tracing back up, we get:
foo(5,9) = 4 * 3 * 2 * 1 * 1 = 24
The function foo is recursive, meaning that it calls itself. Therefore, you should start by looking for its termination point. Based on the definition above, it terminates when either b <= 1 or b <= a. In these two cases, it will return 1.
Now, let us do our trace of the calls:
foo(5,9) = (9-5) * foo(5,8) = 4 * foo(5,8)
foo(5,8) = (8-5) * foo(5,7) = 3 * foo(5,7)
foo(5,7) = (7-5) * foo(5,6) = 2 * foo(5,6)
foo(5,6) = (6-5) * foo(5,5) = 1 * foo(5,5)
foo(5,5) = 1
Therefore, carefully tracing back up, we get:
foo(5,9) = 4 * 3 * 2 * 1 * 1 = 24
Compare your answer with the correct one above
public static int foo(int a) {
if(a <= 3) {
return 1;
}
return a * foo(a - 3) * foo(a - 4);
}
What is the value of the following call to the function defined above:
foo(8)
public static int foo(int a) {
if(a <= 3) {
return 1;
}
return a * foo(a - 3) * foo(a - 4);
}
What is the value of the following call to the function defined above:
foo(8)
The foo method is recursive, so you will need to find the stopping case first. This happens when a <= 3. At that time, the method returns 1. Now, based on this fact, we can begin to trace the method:
foo(8) = 8 * foo(5) * foo(4)
foo(5) = 5 * foo(3) * foo(1)
foo(4) = 4 * foo(1) * foo(0)
foo(0) = foo(1) = foo(3) = 1
Thus, we know:
foo(8) = 8 * 5 * 1 * 1 * 4 * 1 * 1 = 160
The foo method is recursive, so you will need to find the stopping case first. This happens when a <= 3. At that time, the method returns 1. Now, based on this fact, we can begin to trace the method:
foo(8) = 8 * foo(5) * foo(4)
foo(5) = 5 * foo(3) * foo(1)
foo(4) = 4 * foo(1) * foo(0)
foo(0) = foo(1) = foo(3) = 1
Thus, we know:
foo(8) = 8 * 5 * 1 * 1 * 4 * 1 * 1 = 160
Compare your answer with the correct one above
public void draw() {
recurs(10);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What is the output of the code?
public void draw() {
recurs(10);
}
void recurs(int count){
if (count == 0)
return;
else {
System.out.print(count + " ");
int recount = count - 2;
recurs(recount);
return;
}
}
What is the output of the code?
The number 10 is inputed into recurs and prints count,subtracts two from count, then rentered into recurs.
Thus, the first iteration gives, 10.
The second iteration gives as, 10, 8.
It is written this way because it places 8 after the value from the first iteration.
Continuing in this fashion we get,
10 8 6 4 2.
When count reaches 0, nothing is printed.
The number 10 is inputed into recurs and prints count,subtracts two from count, then rentered into recurs.
Thus, the first iteration gives, 10.
The second iteration gives as, 10, 8.
It is written this way because it places 8 after the value from the first iteration.
Continuing in this fashion we get,
10 8 6 4 2.
When count reaches 0, nothing is printed.
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
// ^ 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