1. Which of these expressions evaluates to 5.5?(1 point) I. (double)(11 / 2) II.
ID: 3803055 • Letter: 1
Question
1. Which of these expressions evaluates to 5.5?(1 point)
I. (double)(11 / 2)
II. 11 / (double)2
III. 11 / 2.0
Question 1 options:
2. How many columns are in the array? (1 point)
char[][] array1 = new char[15][10];
Question 2 options:
3. If String str = "The There Here", then what is the value of str.indexOf("he");?(1 point)
Question 3 options:
4. What is the value of vals[4][1]?(1 point)
double[][] vals = {1.1, 1.3, 1.5},
{3.1, 3.3, 3.5},
{5.1, 5.3, 5.5},
{7.1, 7.3, 7.5}
Question 4 options:
5. Consider the method total below.(1 point)
public static int total (int result, int a, int b)
{ if (a == 0)
{
if (b == 0)
{
return result * 2;
}
return result / 2;
}
else
{
return result * 3;
}
}The assignment statementx = total (5, 0, 1);must result in
Question 5 options:
6. Which of the following statements creates alpha, a two-dimensional array of 10 rows and 5 columns, wherein each component is of the type int?(1 point)
int[][] alpha = new int[10][5];
int[][] alpha;
alpha = new int[10][];
for (int i = 0; i < 10; i++)
alpha[i] = new int[5];
Question 6 options:
7. Consider the method total below.(1 point)
public static int total (int result, int a, int b)
{
if (a == 0)
{
if (b == 0)
{
return result * 2;
}
return result / 2;
}
else
{
return result * 3;
}
}
The assignment statement
x = total (5, 0, 0);
must result in
Question 7 options:
8. Consider the following definitions.
public boolean state (int[] list, int value)
{
int counter;
boolean flag = false;
for (counter = 0; counter < list.length; counter++)
{
flag = (list[counter] != value);
}
return flag;
}
Under which of the following conditions must the method above return true ? (1 point)
Question 8 options:
Under all conditions.
Under the condition that value == list[list.length - 1].
Under the condition that value != list[list.length - 1].
Under the condition that value != list[i] for all i such that 0 <= i < list.length.
Under no conditions.
9. What is the output of the program shown below? (1 point)
public class SomeClass
{
private int x, y;
public SomeClass (int xValue, int yValue)
{
x = xValue;
y = yValue;
}
public void m1()
{
x = 30;
System.out.print((y + 1) + " ");
}
public void m2()
{
m1();
System.out.print(x + " ");
}
}
public class Tester
{
public static void main (String[] args)
{
int x = 10;
int y = 20;
SomeClass z = new SomeClass(y, x);
z.m2();
z.m1();
}
}
Question 9 options:
10. What is output by the following code? (1 point)
public class Swapper
{
private int a, b;
public Swapper(int aValue, int bValue)
{
a = aValue;
b = bValue;
}
public void swap ()
{
a = b;
b = a;
}
public void print ()
{
System.out.println("a = " + a + ", and b = " + b);
}
}
public class Tester
{
public static void main(String[] args)
{
Swapper swapObj = new Swapper(10, 20);
swapObj.swap();
swapObj.print();
}
}
Question 10 options:
11. If the value of f(x, y, z) is always an integer, which of the following conditions ensures that the loop below terminates? (1 point)
while (f(x, y, z) < 100)
{
}
Question 11 options:
12. The following program segment is intended to sum a[0] through a[n-1], where n = a.length:
sum = 0;
i = 0;
n = a.length;
while (i != n)
{
i++;
sum += a[i];
}
In order for this segment to perform as intended, which of the following modifications, if any, should be made?(1 point)
Question 12 options:
13.
What is the output of the following program?(1 point)
int sum = 0, k, val = 1;
for (k = 0; k <=4; k++)
{
sum += val;
val++;
}
System.out.println(sum);
Question 13 options:
14. The purpose of a class constructor is to (1 point)
Question 14 options:
15. When is it not necessary to invoke method myMethod using dot notation? (1 point)
Question 15 options:
16.
What is a mutator method? (1 point)
Question 16 options:
17.
What is the output of the following code?(1 point)
ArrayList<Integer> grades = new ArrayList<Integer>();
grades.add(88);
grades.add(92);
grades.add(95);
grades.add(1, 80);
grades.add(83);
System.out.println(grades.get(2 + 3 / 2));
Question 17 options:
18.
Consider the following class designed to store weather statistics at a particular date and time.
public class WeatherSnapshot
{
private int tempInFahrenheit;
private int humidity; // value of 56 means 56% humidity
private int dewpoint; // in degrees Fahrenheit
private Date date; // uses a Date object to store the date
private int time; // in military time, such as 1430 = 2:30 pm
private boolean cloudy; // true if 25% or more of the sky is covered
// constructor not shown, but it initializes all instance variables
// postcondition: returns temperature
public int getTemp()
{
return tempInFahrenheit;
}
// postcondition: returns date
public Date getDate()
{
return date;
}
// postcondition: returns true if precipitation is likely; false otherwise
public boolean precipitationLikely()
{
// implementation not shown
}
// other methods not shown
}
Which of the following is a class constructor? (1 point)
I. getTemp
II. precipitationLikely
III. WeatherSnapshot
Question 18 options:
19.
Consider the following class designed to store weather statistics at a particular date and time.
public class WeatherSnapshot
{
private int tempInFahrenheit;
private int humidity; // value of 56 means 56% humidity
private int dewpoint; // in degrees Fahrenheit
private Date date; // uses a Date object to store the date
private int time; // in military time, such as 1430 = 2:30 pm
private boolean cloudy; // true if 25% or more of the sky is covered
// constructor not shown, but it initializes all instance variables
// postcondition: returns temperature
public int getTemp()
{
return tempInFahrenheit;
}
// postcondition: returns date
public Date getDate()
{
return date;
}
// postcondition: returns true if precipitation is likely; false otherwise
public boolean precipitationLikely()
{
// implementation not shown
}
// other methods not shown
}
Suppose a WeatherSnapshot object named currentWeather has been correctly instantiated in a client class. Which of the following will correctly call the precipitationLikely method? (1 point)
Question 19 options:
20.
What is meant by the term "encapsulation"? (1 point)
Question 20 options:
21.
What is output by the following code fragment?(1 point)
String[] fruits = { "apple", "banana", "peach", "strawberry" };
String str = "a";
for (String item : fruits)
{
str += item.substring(1, 2);
}
System.out.println(str);
Question 21 options:
22.
Consider the following code segment. What would be the expected output?(1 point)
int i = 10;
while (i >= 0)
{
if ((i % 2) > 0)
{
System.out.print(i + " ");
}
i -= 2;
}
Question 22 options:
23.
Assume that the following code exists inside a method of MyClass, and that this code compiles without errors:
int result = book.getYearPublished();
where book is an object of the Book class.
Which of the following is not true about the getYearPublished method?(1 point)
I. It is a mutator method.
II. It is a public method in the Book class.
III. It returns a String.
Question 23 options:
24.
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
}
Explanation / Answer
1- I only i.e (double)(11/2) because all other options contain integers so the final result will be stored as integer.
2- 10 there are 10 coloumns as new char[10][15] so it has 10 rows and 15 coloumns.
3-1 as the first occurance of he is at position 1.
4- there is no such value as we have to get the value of vals[4][1] which means row 4 and coloumn 1 so there is no row with value 4 so there is no such value.
5-x being assigned the value 2 as inner if condition did not satify but outer condition did satisfy so result/2 will be executed.
6-3 both a and b because both declare 2d array with 10 rows and 5 coloumns but second declare it by nesting it.
7-x being assigned the value 5 because first inner if loop is executes which results in result *2 and after outer loop is executed which results in results/2 which computes the original result.
8-Under the condition that value != list[i] for all i such that 0 <= i < list.length.
9-11 30 11 as y=10 so y+1 is printed as 11 x is printed as 30 and then m1 is called again which prints y+1 again that is 11
10-a = 20, and b = 20 because first a is assigned and after that b is assigned a which is already 20 thats is why a and b both are assigned 20.
11-The value of f(x, y, z) is increased during each iteration. because at each iteration value of function is increased and at one point it will be greater then 100 then while condition will terminate.
12-sum = 0; i = 0; should be changed to sum = a[1]; i = 1; because in while condition i is incremented first so it will start with i=1 and then a[0] will not be added to sum so sum should be initialized with a[0].
13-15 as sum=sum+val and val is incremented by 1 everytime so 1+2+3+4+5=15
14-initialize the class instance variables.
15-When myMethod is invoked by a method in the same class as myMethod. as constructor is invoked automatically and constructor is calling the function.
16-A method that modifies an instance variable. as defined by its defination.
17-95 as we have grades.get(2+3/2) which results out to be 3 so arraylist with index 3 and at index 3 is 85.
18=III only as class name is WeatherSnapshot and constructors are of class name only.
19-boolean couldRain = currentWeather.precipitationLikely(); as a boolean variable can only store boolean value as precipitationLikely() will return a boolean value.
20-Encapsulation occurs when client code is allowed to have direct access to instance variables.
21-apaet as it will append letter at index 1 of every string in the string array fruits.
22-Nothing is printed as i=10 when its modulous is taken it will not be greater then 0 it wil be equal to 0 adn after that i is decremented by 2 so it again computes an even value.
23-2 only it is a public method in the book class.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.