Set the following for loop header so that it prints the numbers, 0 2 4 6 8. for
ID: 3782723 • Letter: S
Question
Set the following for loop header so that it prints the numbers, 0 2 4 6 8.
for (int i = ___; i ____; i ___ )
System.out.print(i + " ");
2, < 10, += 2
0, < 10, += 2
2, <= 10, ++
2, <= 10, += 2
0, <= 10, += 2
The following if statement tests the rainfall in New York’s Central Park during the months of June, July and August:
if (low <= rain && rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
Which of the following code segments would produce the exact same output?
I.
if (rain >= low) {
if (rain <= high)
System.out.println("Rainfall amount is normal.");
} else
System.out.println("Rainfall amount is abnormal.");
II.
if (rain >= low) {
if (rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
} else
System.out.println("Rainfall amount is abnormal.");
III.
if (rain >= low)
System.out.println("Rainfall amount is normal.");
else if (rain <= high)
System.out.println("Rainfall amount is normal.");
else
System.out.println("Rainfall amount is abnormal.");
I only
II only
III only
II or III
I, II or III
What does the following code do?
if (month == 4)
if (day <= 21)
System.out.println("Aries");
Prints a message if month is 4 or day is less than or equal to 21.
Prints a message if month is 4 or day is greater than or equal to 21.
Prints a message if month is 4 and day is less than or equal to 21.
Prints a message if month is 4 and day is greater than or equal to 21.
Doesn't work - you cannot have two if statements together.
What is output by the following code segment?
int x = 11;
int y = 11;
if (x != y )
System.out.print("one");
else if (x > y )
System.out.print("two");
else if (y < x)
System.out.print("three");
else if (y >= x)
System.out.print("four");
else
System.out.print("five");
one
two
three
four
five
Which option best completes the truth table for A && !B?
(_1_) 1; (_2_) 1; (_3_) 1; (_4_) 1;
(_1_) 1; (_2_) 1; (_3_) 0; (_4_) 1;
(_1_) 1; (_2_) 0; (_3_) 0; (_4_) 0;
(_1_) 1; (_2_) 0; (_3_) 1; (_4_) 1;
(_1_) 0; (_2_) 1; (_3_) 0; (_4_) 0;
In a class, ______ should be declared as either public or private.
data
constructors
accessors
mutators
methods
Consider the class below:
public class A {
public A() {
System.out.print("one");
}
public A(int x) {
System.out.print("two");
}
}
What is output by the following?
A a = new A();
one
two
onetwo
twoone
nothing
Consider the complete class definition below:
public class Die {
public static void rollIt() {
/* Missing Code */
}
}
Which of the following is the proper way to call the function rollIt() from another class?
Die d = new Die();
d.rollIt();
Die d = new Die();
rollIt(d);
Die.rollIt();
rollIt();
None of the above
Classes use ______ to define their behavior.
constructors
methods
variables
parameters
references
Questions 10 – 12 refer to the following classes definitions:
public class Battery {
private boolean fullyCharged;
private int charge;
private String type;
public Battery (int ch, String ty) {
charge = ch;
if (charge == 100)
fullyCharged = true;
type = ty;
}
public boolean isFullyCharged() {
//returns true if the Battery is fully charged, false otherwise
//implementation not shown
}
//other methods not shown
}//Battery
public class Inventory {
ArrayList<Battery> inventory;
public Inventory (ArrayList<Battery> inv) {
inventory = inv;
}
//other methods not shown
}//Inventory
To add a method that can count how many Battery objects in the ArrayList inventory are charged at less than 50%, which of the following is true?
The method should be implemented in Battery.
The method should be implemented in Battery and Inventory.
The method could be implemented in Battery or Inventory.
The method should be implemented in Inventory.
The method cannot be written because the ArrayList is declared private.
Which accessor method could not be implemented in Battery?
isFullyCharged() //returns true if a Battery is fully charged
getType() //returns the type of a Battery
getInventory() //returns the ArrayList of all Batteries in the inventory
getCharge() //returns the charge of a Battery
equals() //returns true if the type and charge of two Batteries are the same
The following method in Inventory is intended to count how many batteries are fully charged. What should replace /*Missing Code */ so that the method works as intended?
public int countFullyCharged() {
int c = 0;
/* Missing Code */
return c;
}
for (Battery b: inventory)
c++;
for (Battery b: inventory)
if (b.isFullyCharged())
c++;
for (Battery b: inventory)
if (b.charge == 100)
c++;
if (inventory.isFullyCharged())
c++;
None of the above
Methods used to change variables are called ______.
accessors
equals
toString
void
mutators
Write the header for the default constructor for a class called Ship.
private void Ship()
public int Ship()
private Ship()
public Ship()
public void Ship()
Which of the following is the correct way to declare a static variable called x?
int x;
private int x;
x;
private constant int x;
private static int x;
What is printed as a result of executing the following code segment?
ArrayList<String> list = new ArrayList<String>();
list.add ("cookies");
list.add ("nachos");
list.add ("chips");
list.add ("trail mix");
list.add ("celery");
for (String s: list)
if (s.length() > 4 && s.length() < 6)
System.out.print(s.toUpperCase() +" ");
NACHOS CHIPS
CHIPS
chips
nachos chips
NACHOS CHIPS CELERY
Consider the following declaration for an ArrayList:
ArrayList<String> list = new ArrayList<String>();
After values have been added to the array, the following segment processes the ArrayList:
list.add(list.get(0));
list.remove(0);
Which of the following best describes what this segment does?
Adds the last letter in the String onto the beginning.
Move the first String in the ArrayList to the end of the ArrayList.
Removes the first letter in each String in the ArrayList.
Adds the first letter in the String onto the end.
Does not changes the Strings in the ArrayList.
You have written a program to create a grocery list. As each item is placed into your basket you call method called removeItem and it should remove the item from your list. Which of the statements about the code below are true?
public static void removeItem(ArrayList<String> li, String remove) {
for (String s: li)
if (s.equals(remove))
li.remove(s);
}
No changes are made to the ArrayList because the if (s.equals(remove)) is never true.
An exception will be thrown.
The list will have all of the instances of the word passed in as a parameter removed.
Nothing, changes made to object data types are not preserved after method calls.
All elements in the ArrayList are removed.
Consider the following code segment:
ArrayList<Light> bulbs = new ArrayList<Light>();
bulbs.add(new Light());
bulbs.remove(0);
bulbs.add(new Light());
Light b = new Light();
bulbs.add(1, b);
bulbs.add(new Light());
bulbs.remove(0);
bulbs.add(new Light());
bulbs.remove(2);
bulbs.remove(1);
bulbs.add(new Light());
What is the size of bulbs after running the code?
2
3
4
5
6
What goes in between the < > when instantiating a new ArrayList?
A primitive variable
A class data type
A primitive data type
Any data type
A class variable
A B A && !B 1 1 (_1_) 1 0 (_2_) 0 1 (_3_) 0 0 (_4_)Explanation / Answer
Note: the numbering of all the questions has been done as 1)., 2)., …
1).
The given for loop needs to print a sequence of numbers such as 0, 2, 4, 6, 8. This sequence can be displayed by filling the required fields of the given for loop as follows:
In the given sequence, 0 is displayed first, therefore the for loop should be started with i = 0. There is a difference of 2 between each number and its previous number. Thus, the increment condition of the for loop will be filled with i += 2.
The given sequence will be displayed till the number is less than 10. Thus, the test condition of the given for loop will be filled with i < 10.
Hence, the correct choice is 0, < 10, += 2.
2).
The given if/else statement will test if the rainfall is greater than or equal to low and it is less than or equal to high, then it will display that rainfall amount is normal. Otherwise, the rainfall amount is abnormal.
The if/else statement in the second option will produce exactly same output as above. The first if statement will check whether the rainfall is greater than or equal to low.
If the condition satisfied, then the flow of if/else reached to the second nested if statement and checks whether the rainfall is less than or equal to high. If this condition is satisfied, then the rainfall is normal. Otherwise, the rainfall is abnormal.
Hence, the correct choice is II only.
3).
The given nested if structure can be explained as follows:
Hence, the correct choice is that the given if/else statement will print a message if the month is 4 and day is less than or equal to 21.
4).
The given code segment will compare the values of x and y using if/else statements. The flow of the code segment is explained as follows:
Sice the values of x and y are same i.e., 11. Thus, the else if statement which check whether the vale of x is less than or equal to the value of y will be executed because the value of x is equal to value of y and the output will be displayed as “four”.
Hence, the correct choice is four.
5).
The given truth table will display the result of logic A && !B. The result of this logic can be explained as follows:
Hence, the correct choice is (_1_) 0; (_2_) 1; (_3_) 0; (_4_) 0;.
6).
The methods are called as member functions of the class. The member functions of the class can be declared as either public or private but for the sake of encapsulation.
Hence, the correct choice is methods.
7).
The constructor of the class will be invoked when an object of the same class is created. If there are no arguments passed to the constructor while creating the object, then the default constructor will be invoked.
Since the object of the class A is created without passing any arguments to the parameterized constructor of the class. Thus, the default constructor of the class A is invoked and it prints a message “one”.
Hence, the correct choice is one.
8).
The function declared or defined in a class is called as member function of the class. The member function of the class can be called as normally within the same class.
But if the member function of a class needs to be called from another class, then it can be called by the object of that class or name of that class.
The function rollt() can be called by the name of the class like Die.rollt(). The correct choice is Die.rollt().
9).
The behavior of the class represents the functionality of that class. The methods of the class are used to represent the functionality of the class.
Hence, the correct choice is methods.
10).
The variable inventory of the class ArrayList is declared in the class Inventory and the contents of the array list inventory can be iterated in the class Inventory.
Hence, the correct choice is the method should be implemented in Inventory.
11).
The method getInventory() cannot be implemented in the class Battery because the variable inventory is an array list of the objets of the class battery and declared in the class Inventory.
Hence, the correct choice is getInventory().
12).
The missing code can be filled as follows:
The for loop in the second choice will iterate over all the batteries and increment te count of batteries by one if it is fully charged.
Hence, the correct choice is for(Battery b: inventory) if(b.isFullyCharged()) c++;.
13).
The methods which can be used to change the values of the variables of the class are called as mutators. These methods will set the values of the variables and are helpful in case of accessing the private member variables of the class.
Hence, the correct choice is mutators.
14).
The default constructor can be declared as follows:
The default constructor of the class Ship can be declared as public Ship().
Hence, the correct choice is public Ship().
15).
The correct way to declare a static variable is shown as follows:
private static int x;
The name of the variable is x, data type of the variable is int, access specifier of the variable is static, and the variable is static.
Hence, the correct choice is private static int x.
16).
The array list list will have a list of strings. The statements list.add() will add the strings into the array list (list) of strings one by one. The for loop will be used to iterate over the list items.
In each iteration, if the length of the string is in between 4 and 6, then only the string will be displayed in upper case. The string chips is the only string in the list of strings which has length between 4 and 6. Thus, the output of the program will be CHIPS.
Hence, the correct choice is CHIPS.
17).
The statement list.add(list.get(0)) will get the string at the first index i.e., 0 and add it to the last index of the array list. The second statement list.remove(0) will remove the list item at index 0. These statements will shift the first string to the end of the list.
Hence, the correct choice is move the first string in the ArrayList to the end of the ArrayList.
18).
The loop will goes past the end of the array list that’s why there will be an exception.
Hence, the correct choice is an exception will be thrown.
19).
The explanation of the flow of the code segment is shown as follows:
Hence, the correct choice is 2.
20).
The array list can store the objects of any class. If there is a class named as A, then an array list of objects of the class A can be declared as follows:
ArrayList<A> list = new ArrayList<A>();
The class data type should be stored in between the < > when instantiating a new ArrayList.
Hence, the correct choice is a class data type.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.