Standard Data Structures - AP Computer Science A
Card 0 of 935
Write a program that iterates through this data structure and prints the data (choose the best answer):
List<List<String>> listOflistOfStrings = new ArrayList<ArrayList<String>>();
Write a program that iterates through this data structure and prints the data (choose the best answer):
List<List<String>> listOflistOfStrings = new ArrayList<ArrayList<String>>();
The correct answer uses a ForEach loop. A ForEach loop is recommended for iterating through Lists because Lists contain iterators. ForEach loops use the iterator to iterate through the List. One of the answers used a regular For loop, while the answer was correct, it was not the best choice.
The correct answer uses a ForEach loop. A ForEach loop is recommended for iterating through Lists because Lists contain iterators. ForEach loops use the iterator to iterate through the List. One of the answers used a regular For loop, while the answer was correct, it was not the best choice.
Compare your answer with the correct one above
Consider the code below:
String\[\] db = {"Harvey","Plutarch","Frege","Radulphus"};
ArrayList names = new ArrayList();
for(int i = 0; i < 12; i++) {
names.add(db\[i % db.length\]);
}
for(int i = 0; i < 12; i++) {
if(names.get(i).equals("Frege")) {
names.remove(i);
}
}
What is the bug in the code above?
Consider the code below:
String\[\] db = {"Harvey","Plutarch","Frege","Radulphus"};
ArrayList
for(int i = 0; i < 12; i++) {
names.add(db\[i % db.length\]);
}
for(int i = 0; i < 12; i++) {
if(names.get(i).equals("Frege")) {
names.remove(i);
}
}
What is the bug in the code above?
In the second loop, when you remove one of the items, you thus make the ArrayList one element shorter. This means that if you attempt to go through to index 11, you will receive an error at some point, for the list will have "shrunk". This is like an array out of bounds error, though it will be an IndexOutOfBoundsException exception.
In the second loop, when you remove one of the items, you thus make the ArrayList one element shorter. This means that if you attempt to go through to index 11, you will receive an error at some point, for the list will have "shrunk". This is like an array out of bounds error, though it will be an IndexOutOfBoundsException exception.
Compare your answer with the correct one above
Consider the following code:
public static class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getArea() {
return radius * radius * Math.PI;
}
}
public static void main(String\[\] args) {
ArrayList circles = new ArrayList();
for(int i = 0; i < 10; i++) {
circles.add(new Circle(i + 4 * 2));
}
}
Which of the following represents code for iterating through the list circles in order to output the areas of the circles contained therein?
Consider the following code:
public static class Circle {
private double radius;
public Circle(double r) {
radius = r;
}
public double getArea() {
return radius * radius * Math.PI;
}
}
public static void main(String\[\] args) {
ArrayList
for(int i = 0; i < 10; i++) {
circles.add(new Circle(i + 4 * 2));
}
}
Which of the following represents code for iterating through the list circles in order to output the areas of the circles contained therein?
In the answers, there are only two issues to consider. On the one hand, look at the loop control statement. It is some variant on:
circle.size()
and
circles.length
For the List types, you need to use the method size(), not length (which you use for arrays). Likewise, one way to extract elements from the ArrayList type is to use the .get method. You must use this and not the brackets \[\] that you use for arrays.
In the answers, there are only two issues to consider. On the one hand, look at the loop control statement. It is some variant on:
circle.size()
and
circles.length
For the List types, you need to use the method size(), not length (which you use for arrays). Likewise, one way to extract elements from the ArrayList type is to use the .get method. You must use this and not the brackets \[\] that you use for arrays.
Compare your answer with the correct one above
Consider the code below:
ArrayList myPhilosophers = new ArrayList();
myPhilosophers.add("Frege");
myPhilosophers.add("Husserl");
myPhilosophers.add("Hegel");
myPhilosophers.add("Bill");
myPhilosophers.add("Frederick");
for(int i = 0; i < myPhilosophers.size(); i++) {
String s = myPhilosophers.get(i);
if(s.charAt(0) >= 'H' || s.charAt(3) < 'd') {
myPhilosophers.set(i, s.toUpperCase());
}
}
System.out.println(myPhilosophers);
What is the output for the code above?
Consider the code below:
ArrayList
myPhilosophers.add("Frege");
myPhilosophers.add("Husserl");
myPhilosophers.add("Hegel");
myPhilosophers.add("Bill");
myPhilosophers.add("Frederick");
for(int i = 0; i < myPhilosophers.size(); i++) {
String s = myPhilosophers.get(i);
if(s.charAt(0) >= 'H' || s.charAt(3) < 'd') {
myPhilosophers.set(i, s.toUpperCase());
}
}
System.out.println(myPhilosophers);
What is the output for the code above?
Consider the logic that is in the loop. The if statement will be reached either if you:
- Have a first character that is H or later in the alphabet (in capital letters).
- Have a fourth character that is less than d.
The first case applies to "Husserl" and "Hegel." However, the second does not apply to any—not even to "Frederick", for d is equal to d, not less than it!
Consider the logic that is in the loop. The if statement will be reached either if you:
- Have a first character that is H or later in the alphabet (in capital letters).
- Have a fourth character that is less than d.
The first case applies to "Husserl" and "Hegel." However, the second does not apply to any—not even to "Frederick", for d is equal to d, not less than it!
Compare your answer with the correct one above
Which of the following is NOT a valid declaration for an ArrayList?
Which of the following is NOT a valid declaration for an ArrayList?
ArrayLists are lists of objects, and thus cannot store primitive types. If you wish to store primitive types in an ArrayList, container objects such as Double or Integer must instead be used. The object reference type List is a valid reference type for an ArrayList object.
ArrayLists are lists of objects, and thus cannot store primitive types. If you wish to store primitive types in an ArrayList, container objects such as Double or Integer must instead be used. The object reference type List is a valid reference type for an ArrayList object.
Compare your answer with the correct one above
Which of the following is NOT a difference between the Array class and the ArrayList class in Java?
Which of the following is NOT a difference between the Array class and the ArrayList class in Java?
In Java, Array is a fixed length data structure, while ArrayList is a variable length Collection Class (other Collection class members include HashMap and HashSet). This means that an Array cannot change its size once it's made; a whole new Array must be made. ArrayLists can change size at will.
Arrays can reference both primitives (like int) and types (like Integer). ArrayLists can only reference types, not primitives. Through a method called "Autoboxing", it can appear that an ArrayList stores a primitive, but what it really is doing is automatically masking the primitive with the type it's like so that it works with the ArrayList.
During the creation of an Array, the .length value is assigned, so whenever you append .length to the end of an array, you get the length (which will never change for the given Array). For ArrayLists, you use the .size() function, which returns the length of the ArrayList. Because the ArrayList size is variable, it is a function (which is why the parenthesis are there at the end).
Java provides the add() function for ArrayLists, which, as the name implies, adds the argument to the ArrayList. Arrays, on the other hand, do not have functions to add elements. To add something to an Array, you'd call something similar to this:
int[] my_array = new int[10];
my_array[0] = 5;
In Java, Array is a fixed length data structure, while ArrayList is a variable length Collection Class (other Collection class members include HashMap and HashSet). This means that an Array cannot change its size once it's made; a whole new Array must be made. ArrayLists can change size at will.
Arrays can reference both primitives (like int) and types (like Integer). ArrayLists can only reference types, not primitives. Through a method called "Autoboxing", it can appear that an ArrayList stores a primitive, but what it really is doing is automatically masking the primitive with the type it's like so that it works with the ArrayList.
During the creation of an Array, the .length value is assigned, so whenever you append .length to the end of an array, you get the length (which will never change for the given Array). For ArrayLists, you use the .size() function, which returns the length of the ArrayList. Because the ArrayList size is variable, it is a function (which is why the parenthesis are there at the end).
Java provides the add() function for ArrayLists, which, as the name implies, adds the argument to the ArrayList. Arrays, on the other hand, do not have functions to add elements. To add something to an Array, you'd call something similar to this:
int[] my_array = new int[10];
my_array[0] = 5;
Compare your answer with the correct one above
Consider the following code:
double a = 4.5, d = 4;
int b = 10,c=5;
double e = a / b + c / d - d % c;
What is the value of e at the end of this code's execution?
Consider the following code:
double a = 4.5, d = 4;
int b = 10,c=5;
double e = a / b + c / d - d % c;
What is the value of e at the end of this code's execution?
The easiest way to work through this code is to comment on its parts. See the comments below in bold.
double a = 4.5, d = 4;
int b = 10,c=5;
/*
a / b: This is a division that will maintain the decimal portion (since it has a double involved in it). 4.5 / 10 will evaluate to 0.45.
c / d: Since this has both an int and a double, it will evaluate to a double. That means that it will maintain its decimal portion as well. It is: 5 / 4 or 1.25.
d % c: Since d is 4, this kind of remainder division works just like having two integers for the modulus. 4 % 5 is just 4. (It is 0 remainder 4.)
Thus, the expression is: 0.45 + 1.25 - 4, which is -2.3.
*/
double e = a / b + c / d - d % c;
The easiest way to work through this code is to comment on its parts. See the comments below in bold.
double a = 4.5, d = 4;
int b = 10,c=5;
/*
a / b: This is a division that will maintain the decimal portion (since it has a double involved in it). 4.5 / 10 will evaluate to 0.45.
c / d: Since this has both an int and a double, it will evaluate to a double. That means that it will maintain its decimal portion as well. It is: 5 / 4 or 1.25.
d % c: Since d is 4, this kind of remainder division works just like having two integers for the modulus. 4 % 5 is just 4. (It is 0 remainder 4.)
Thus, the expression is: 0.45 + 1.25 - 4, which is -2.3.
*/
double e = a / b + c / d - d % c;
Compare your answer with the correct one above
What line number makes the following Python Code not execute? (1:, 2:, 3:,... represents line numbers)
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is "+z\[3\]
10: print(string)
What line number makes the following Python Code not execute? (1:, 2:, 3:,... represents line numbers)
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is "+z\[3\]
10: print(string)
When the code is executed it will produce a type error with the variable string in line 9. The variable string is trying to concatenate a string and an integer from the z array. To make the code executable, we would need to make the integer from the z array a string. We would do this by simply wrapping it in the method str(), which converts it into a string. After making the change, the following code will execute correctly.
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is"+str(z\[3\])
10: print(string)
When the code is executed it will produce a type error with the variable string in line 9. The variable string is trying to concatenate a string and an integer from the z array. To make the code executable, we would need to make the integer from the z array a string. We would do this by simply wrapping it in the method str(), which converts it into a string. After making the change, the following code will execute correctly.
1: x=\[\]
2: y=\[\]
3: z=\[\]
4: for i in range(10):
5: x.append(i)
6: y.append(x\[i\]+2*x\[i\])
7: z.append(x\[i\]+y\[i\]**2)
8: print(z)
9: string="The fourth element of z is"+str(z\[3\])
10: print(string)
Compare your answer with the correct one above
What is the size of the float data type in Java?
What is the size of the float data type in Java?
A float is represented by 4 bytes (32 bits). It's comprised of 1 sign bit, 8 exponent bits, and 23 mantissa bits. An example of a floating point number in binary would be 11111111111111111111111111111111. The breakdown would be [1](sign bit) [11111111](exponent bits) [11111111111111111111111](mantissa bits). All in all, that equals 32 bits.
In Java, the byte is the only 8 bit data type. The short and char are 16 bits in size. There are no 48 bit data types, as everything is in a power of 2. The double and long are 64 bits in size.
Because floats have half the number of bits as doubles, it isn't as precise, so shouldn't be used when lots of precision is needed. It's better suited for when there are memory size concerns.
A float is represented by 4 bytes (32 bits). It's comprised of 1 sign bit, 8 exponent bits, and 23 mantissa bits. An example of a floating point number in binary would be 11111111111111111111111111111111. The breakdown would be [1](sign bit) [11111111](exponent bits) [11111111111111111111111](mantissa bits). All in all, that equals 32 bits.
In Java, the byte is the only 8 bit data type. The short and char are 16 bits in size. There are no 48 bit data types, as everything is in a power of 2. The double and long are 64 bits in size.
Because floats have half the number of bits as doubles, it isn't as precise, so shouldn't be used when lots of precision is needed. It's better suited for when there are memory size concerns.
Compare your answer with the correct one above
Breanna wants to keep track of how many grapes she eats in a day over the course of a week.
Her brother Nathan, however, wants to keep track of the average amount of grapes he eats in a day over the course of a week.
What type of variable would best be fit for what Breanna wants to keep track of? What about Nathan?
Breanna wants to keep track of how many grapes she eats in a day over the course of a week.
Her brother Nathan, however, wants to keep track of the average amount of grapes he eats in a day over the course of a week.
What type of variable would best be fit for what Breanna wants to keep track of? What about Nathan?
Breanna should use int because she would be counting whole numbers.
Nathan however, should use double because he will be adding up how many grapes he eats and divide by the number of days in the week.
Breanna should use int because she would be counting whole numbers.
Nathan however, should use double because he will be adding up how many grapes he eats and divide by the number of days in the week.
Compare your answer with the correct one above
How do we declare a method to return a Double in Swift (iOS)?
How do we declare a method to return a Double in Swift (iOS)?
In Swift, all methods must first say what they are. They are functions, so they are prefixed with func. Next, methods must have a name. In this case, we named it method. All methods need to specify parameters, even if there are no parameters. So, method() i.e. no parameters. Finally, we wanted to return a Double. So we set the return type using -> Double.
In Swift, all methods must first say what they are. They are functions, so they are prefixed with func. Next, methods must have a name. In this case, we named it method. All methods need to specify parameters, even if there are no parameters. So, method() i.e. no parameters. Finally, we wanted to return a Double. So we set the return type using -> Double.
Compare your answer with the correct one above
On a standard binary tree, what would the data structure look like if we inserted the following names into the tree, supposing that names are compared in a standard lexicographic order:
Isaac, Henrietta, Nigel, Buford, Jethro, Cletus
On a standard binary tree, what would the data structure look like if we inserted the following names into the tree, supposing that names are compared in a standard lexicographic order:
Isaac, Henrietta, Nigel, Buford, Jethro, Cletus
A standard Binary Tree inserts into the root first. It then tries to insert to the "left" for values that are smaller and to the "right" for values that are larger. Therefore, for the data given, we have the first step:

Next, you will insert "Henrietta" to the left, for that is less than "Isaac":

Next, "Nigel" is greater than "Isaac":

Then, "Buford" is less than "Isaac" and then less than "Henrietta":

This continues through the following stages:


Thus, the last image is your final tree!
A standard Binary Tree inserts into the root first. It then tries to insert to the "left" for values that are smaller and to the "right" for values that are larger. Therefore, for the data given, we have the first step:

Next, you will insert "Henrietta" to the left, for that is less than "Isaac":

Next, "Nigel" is greater than "Isaac":

Then, "Buford" is less than "Isaac" and then less than "Henrietta":

This continues through the following stages:


Thus, the last image is your final tree!
Compare your answer with the correct one above
POST-ORDER TRAVERSAL
GIVEN THE FOLLOWING TREE:

WHAT IS THE RESULT OF A POST ORDER TRAVERSAL?
POST-ORDER TRAVERSAL
GIVEN THE FOLLOWING TREE:

WHAT IS THE RESULT OF A POST ORDER TRAVERSAL?
When doing a post-order traversal we go in the following order:
left, right, root.
This means that we are doing any left nodes first, then the right nodes, and LASTLY, the root nodes. If a node is a parent, then we must go throught the left and right children first. Since we're doing post-order traversal, the main root is going to be LAST.
In our example, 1 is a parent so we go to it's left child who is also a parent to node 4. This means that 4 is our first number in the traversal.
POST ORDER TRAVERSAL: 4
Since node 2 doesn't have a right child, we then make that node our next number in the traversal since node 2 it's the left child of node 1.
POST ORDER TRAVERSAL: 4, 2
By now, we have traversed the left nodes of the root. Now we move on to the right subtrees of node 1. Since node 3 is a parent node, we go to it's left child first (node 6). Since node 6 is also a parent, we move on to its children. Node 6 doesn't have a left child. Therefore our next number in the traversal is its right child (node 8) and then the subtree's root (node 6).
POST ORDER TRAVERSAL: 4, 2, 8, 6
Now, we've traversed the left children of node 3 we need to traverse the right child (node 7) who doesn't have any children of its own.
POST ORDER TRAVERSAL: 4, 2, 8, 6, 7
Lastly since we traversed it's right child, we move to the parent and traverse node 3 and our main root (node 1).
POST ORDER TRAVERSAL: 4, 2, 8, 6, 7, 3, 1
When doing a post-order traversal we go in the following order:
left, right, root.
This means that we are doing any left nodes first, then the right nodes, and LASTLY, the root nodes. If a node is a parent, then we must go throught the left and right children first. Since we're doing post-order traversal, the main root is going to be LAST.
In our example, 1 is a parent so we go to it's left child who is also a parent to node 4. This means that 4 is our first number in the traversal.
POST ORDER TRAVERSAL: 4
Since node 2 doesn't have a right child, we then make that node our next number in the traversal since node 2 it's the left child of node 1.
POST ORDER TRAVERSAL: 4, 2
By now, we have traversed the left nodes of the root. Now we move on to the right subtrees of node 1. Since node 3 is a parent node, we go to it's left child first (node 6). Since node 6 is also a parent, we move on to its children. Node 6 doesn't have a left child. Therefore our next number in the traversal is its right child (node 8) and then the subtree's root (node 6).
POST ORDER TRAVERSAL: 4, 2, 8, 6
Now, we've traversed the left children of node 3 we need to traverse the right child (node 7) who doesn't have any children of its own.
POST ORDER TRAVERSAL: 4, 2, 8, 6, 7
Lastly since we traversed it's right child, we move to the parent and traverse node 3 and our main root (node 1).
POST ORDER TRAVERSAL: 4, 2, 8, 6, 7, 3, 1
Compare your answer with the correct one above
int a = 49;
int b = 6;
``
int c = 49 % 6;
What is the value of c?
int a = 49;
int b = 6;
``
int c = 49 % 6;
What is the value of c?
The modulus (or modulo) operator returns the remainder of a division statement. It only works with integer operands. Thus, it will only return an integer result. Six is able to divide forty-eight a maximum of eight times.

And then

Thus,

The modulus (or modulo) operator returns the remainder of a division statement. It only works with integer operands. Thus, it will only return an integer result. Six is able to divide forty-eight a maximum of eight times.
And then
Thus,
Compare your answer with the correct one above
int a = 5;
a += 3.14;
Using the language C++ or Java, what will the value of a be after the above code snippet?
int a = 5;
a += 3.14;
Using the language C++ or Java, what will the value of a be after the above code snippet?
When using the plus-equals (+=) operator, the answer is cast to the type of the left-hand argument. Since a was an int, the value returned by the expression is converted to int before it is stored in a. Thus, the decimal point and any information behind the decimal is truncated.
When using the plus-equals (+=) operator, the answer is cast to the type of the left-hand argument. Since a was an int, the value returned by the expression is converted to int before it is stored in a. Thus, the decimal point and any information behind the decimal is truncated.
Compare your answer with the correct one above
Consider the following code:
int i = 85, j = 2, k = 4;
i--;
j++;
k -= 6;
i = i / j + i / k;
What is the value of i in the code above?
Consider the following code:
int i = 85, j = 2, k = 4;
i--;
j++;
k -= 6;
i = i / j + i / k;
What is the value of i in the code above?
It is probably easiest to give a running commentary on the code in order to understand it. See the comments in bold below:
int i = 85, j = 2, k = 4;
i--; // After this, i = 84
j++; // After this, j = 3
k -= 6; // After this, k = k - 6 or k = -2
/*
Consider this in several steps:
i / j = 84 / 3 = 28
i / k = 84 / -2 = -42
Thus, the final expression is:
i = 28 - 42 = -14
*/
i = i / j + i / k;
It is probably easiest to give a running commentary on the code in order to understand it. See the comments in bold below:
int i = 85, j = 2, k = 4;
i--; // After this, i = 84
j++; // After this, j = 3
k -= 6; // After this, k = k - 6 or k = -2
/*
Consider this in several steps:
i / j = 84 / 3 = 28
i / k = 84 / -2 = -42
Thus, the final expression is:
i = 28 - 42 = -14
*/
i = i / j + i / k;
Compare your answer with the correct one above
Consider the code below:
int a = 24, b = 17, c = 5, d = 7;
b = b % d + a / b;
a %= b;
a -= 12;
What is the value of a at the end of the code above?
Consider the code below:
int a = 24, b = 17, c = 5, d = 7;
b = b % d + a / b;
a %= b;
a -= 12;
What is the value of a at the end of the code above?
The easiest way to trace code like this is to provide running commentary in bold comments. See these below:
int a = 24, b = 17, c = 5, d = 7;
/*
The modulus (%) is the remainder of a division. So . . .
b % d = 17 % 7 = 3 (17 divided by 7 is 2 remainder 3.)
a / b = 1 (Remember, integer division truncates the decimal portion.)
Thus, we have:
b = 3 + 1 = 4
*/
b = b % d + a / b;
a %= b; // This is the same as a = a % d, which is a = 24 % 4 = 0 (No remainder)
a -= 12; // Remember that this is the same as a = a - 12, which is a = 0-12 or -12.
The easiest way to trace code like this is to provide running commentary in bold comments. See these below:
int a = 24, b = 17, c = 5, d = 7;
/*
The modulus (%) is the remainder of a division. So . . .
b % d = 17 % 7 = 3 (17 divided by 7 is 2 remainder 3.)
a / b = 1 (Remember, integer division truncates the decimal portion.)
Thus, we have:
b = 3 + 1 = 4
*/
b = b % d + a / b;
a %= b; // This is the same as a = a % d, which is a = 24 % 4 = 0 (No remainder)
a -= 12; // Remember that this is the same as a = a - 12, which is a = 0-12 or -12.
Compare your answer with the correct one above
What is the Unicode value for the letter "W"?
What is the Unicode value for the letter "W"?
The Unicode Standard is Java's attempt to simplify the translation of characters and symbols to input the computer can understand, while being able to work on all Java machines. The first 128 numbers (0-127, 0x0000-0x007F) are for basic Latin. Under this character encoding scheme, the letter 'W' is denoted by U+0057.
The standard for writing Unicode values is U+####, where the number is in hexadecimal format, not a base 10 number.
The Unicode Standard is Java's attempt to simplify the translation of characters and symbols to input the computer can understand, while being able to work on all Java machines. The first 128 numbers (0-127, 0x0000-0x007F) are for basic Latin. Under this character encoding scheme, the letter 'W' is denoted by U+0057.
The standard for writing Unicode values is U+####, where the number is in hexadecimal format, not a base 10 number.
Compare your answer with the correct one above
#include
using namespace std;
int main()
{
bool bin=true;
int var=bin*2 +1;
int triple=5;
int final= (bin&var) &triple;
cout<<final;
return 0;
}
What is the output of this code?
#include
using namespace std;
int main()
{
bool bin=true;
int var=bin*2 +1;
int triple=5;
int final= (bin&var) &triple;
cout<<final;
return 0;
}
What is the output of this code?
Taking a look at the first line in main, the variable bin is defined as true, which we know to be 1. We see in the next line that the variable var is 2 * bin +1. The trick here is that you can actually do math with boolean values, so the value of var is 3.
The value of triple is 5.
The the variable final asks us to AND bin and var first, then AND triple.
To do this, we can convert all three numbers into binary notation to make it easier.
1 in binary is 001.
3 in binary is 011.
5 in binary is 101.
1&3 =1.
1&5=1.
Final answer is 1.
Taking a look at the first line in main, the variable bin is defined as true, which we know to be 1. We see in the next line that the variable var is 2 * bin +1. The trick here is that you can actually do math with boolean values, so the value of var is 3.
The value of triple is 5.
The the variable final asks us to AND bin and var first, then AND triple.
To do this, we can convert all three numbers into binary notation to make it easier.
1 in binary is 001.
3 in binary is 011.
5 in binary is 101.
1&3 =1.
1&5=1.
Final answer is 1.
Compare your answer with the correct one above
int a;
a= 1+1 * 2 + 4 / 5;
System.out.println(a);
What would the code output?
int a;
a= 1+1 * 2 + 4 / 5;
System.out.println(a);
What would the code output?
The expression 1+1 * 2 + 4 / 5 can be rewritten as
1 + (1*2) + (4/5) = 1 + 2 + 0
4/5 = 0 because a is an int and when dividing ints, there are no decimals and since 4/5=0.8 we keep the ones digit which is zero.
The expression 1+1 * 2 + 4 / 5 can be rewritten as
1 + (1*2) + (4/5) = 1 + 2 + 0
4/5 = 0 because a is an int and when dividing ints, there are no decimals and since 4/5=0.8 we keep the ones digit which is zero.
Compare your answer with the correct one above