Complete the Car class shown below. A Car has a fuel tank level, and a miles per
ID: 3810094 • Letter: C
Question
Complete the Car class shown below. A Car has a fuel tank level, and a miles per gallon value. Assume that all cars have 12 gallon gas tanks. (20 points)
public class Car
{
// declare your constants and instance variables here
// postcondition: all instance variables are initialized
public Car(double mpgValue, double gallons)
{
}
// postcondition: the fuel level is increased by the amount given by the parameter public void addFuel(double gallons)
{
}
// postcondition: the fuel tank level is decreased by the amount of gas
// that it would take this Car to drive the number of miles given by the parameter
// The fuel level cannot be negative.
public void drive(double miles)
{
}
// other methods not shown
}
7.
Given the following code:
if (n == 2)
{
k -= 2;
}
else if (n == 3)
{
k -= 3;
}
can be rewritten as if (< condition >)
{
< assignment statement >;
}
where < condition > and < assignment statement > are chosen so that the rewritten code
performs the same task as the original code. Assume that both n and k are integer
variables.
Which of the following could be used as < condition >? (2 points)
I. (n == 2) || (n == 3)
II. (n == 2) && (n == 3)
III. (n >= 2) && (n <= 3)
Question 7 options:
8.
What is stored in str after the following code executes? (2 points)
String str = "Computer Science";
int i = 0;
while (i < 8) {
if (str.indexOf("m") < i) {
str = str.substring(0, 2) + str;
}
i += 2;
}
Question 8 options:
9.
int i = 100;
int j = 10;
while (i > 0)
{
i = i / j;
j = 1 + j % 5;
}
What is the value of i after this code executes? (2 points)
Question 9 options:
10.
Consider the following code:
String[ ] words = {"cat", "dog", "lamb", "giraffe", "elephant"};
int i = 0, index = -1, temp;
while (i < words.length) {
temp = words[i].indexOf("a");
if (temp > index) {
index = temp;
}
i++;
}
What is the value of index after this code executes? (2 points)
Question 10 options:
Consider the following code examples, where all variables are of type int.
Example 1
x = n;
y = x;
while (x > 0) {
y += 1;
x /= 2;
}
Example 2
x = n;
y = x;
if (x > 0) {
while (x > 1) {
y += 1;
x /= 2;
}
}
Assume that the two examples start with the same value for variable n. For which
value(s) of n do the two code examples compute the same value for variable y ? (2 points)
I.Any value less than zero
II.The value zero
III.Any value greater than zero
Question 11 options:
12.
Suppose the following array is declared:
int[ ] grades = {88, 92, 95, 83};
What is the value ofgrades[3]? (2 points)
Question 12 options:
13.
Suppose the following array is declared:
int[ ] grades = {88, 92, 95, 83};
and the following integer is declared:
int index = 10 - 2 * 4;
What is the value of grades[index]? (2 points)
Question 13 options:
14.
Suppose the following array is declared:
int[ ] grades = {88, 92, 95, 83};
What are the values in grades after the following code executes? (2 points)
grades[0] = grades[1];
grades[1] = grades[0];
Question 14 options:
15.
Suppose the following array is declared:
int[ ] grades = {88, 92, 95, 83};
What is the value of grades[grades[0] / 22]? (2 points)
Question 15 options:
1) I only 2) II only 3) III only 4) I and III only 5) II and III onlyExplanation / Answer
15.
Option 1 An ArrayIndexOutOfBoundsException occurs
because grades[0] / 22 in is 4 , so grades[grades[0] / 22] will be grades[4] as the range is only 3 it will therw the exception
----------
14.
Option 4 {92, 92, 95, 83}
becase grades[0] = grades[1]; will assign value of 1 (92) in oth position
and grades[1] = grades[0]; will assign value of 1(92) in first postion.
so answer will be {92, 92, 95, 83}
----------
13
Option 4. 95
because int index = 10 - 2 * 4; here index value will 2.
so grades[index] will be grades[2]. so in array 2th index valus is 95.
-----------
12.
Option 5. 83
as grades[3] in array is 83 only.
--------------
11.Option 4
I and II only
------------
10. Otion 3
1
-------------------
9.Option 4.
5
--------------------
8. option 3
CoCoComputer Science
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.