AP Computer Science A › Double
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)
Line 9
Line 5
Line 8
Line 6
Line 7
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)
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)
Line 9
Line 5
Line 8
Line 6
Line 7
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)
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)
Line 9
Line 5
Line 8
Line 6
Line 7
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)
How do we declare a method to return a Double in Swift (iOS)?
func method() -> Double {}
method() -> Double {}
func method -> Double {}
func -> 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.
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?
There is a type-related error in the code.
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;
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?
There is a type-related error in the code.
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;
How do we declare a method to return a Double in Swift (iOS)?
func method() -> Double {}
method() -> Double {}
func method -> Double {}
func -> 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.
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?
There is a type-related error in the code.
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;
How do we declare a method to return a Double in Swift (iOS)?
func method() -> Double {}
method() -> Double {}
func method -> Double {}
func -> 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.
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
Nathan should use double
Breanna should use int
Nathan should use int
Breanna should use double
Nathan should use double
Breanna should use double
Nathan should use int
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.