Java SE 11: Expressions, Arrays, & Loops Flashcards
Select the possible values returned by a Boolean expression.
▢ 1
▢ 0
▢ true
▢ 2
▢ false
▢ no
▢ yes
1
0
☑ true
2
☑ false
no
yes

C Less than or equal to
A Equal to
B Not equal to
D Greater than or equal to
Select the response which simplifies the following code.
boolean largeVenue;
if (attendees >= 5) {
largeVenue = true;
}
else {
largeVenue = false;
}
largeVenue = (attendees * true + 5);
largeVenue = (attendees *= 5);
largeVenue = (attendees >= 5);
largeVenue = attendees;
largeVenue = (attendees >= 5);
What is the output printed by the following code?
int hoursNeeded = 5;
int hoursAvailable = 5;
int hoursRemaining;
if (hoursAvailable > 0) {
hoursRemaining = hoursAvailable - hoursNeeded;
if(hoursRemaining < 0) {
System.out.println(“Not enough time.”);
} else {
String suffix = “”;
if(hoursRemaining != 1) {
suffix = “s”;
}
System.out.println(“You will have “ + hoursRemaining + “ hour” + suffix + “ remaining.”);
}
} else {
System.out.println(“Times up.”);
}
You will have 0 hours remaining.
You will have 5 hours remaining.
Not enough time.
Times up.
You will have 1 hour remaining.
You will have 0 hours remaining.
Which property or method is used to get the number of elements in an array?
sizeof
lengthof
length
size
length
Which of the following will initialize the names array to be three elements?
▢ String[] names = new[3];
▢ String[] names = new String[3];
▢ String[] names = String * 3;
▢ String[] names = { “Steve”, “Mary”, “William” };
▢ String[] names = (1, 2, 3);
▢ String names = new String(“3”);
▢ String[] names = new[3];
√ String[] names = new String[3];
▢ String[] names = String * 3;
√ String[] names = { “Steve”, “Mary”, “William” };
▢ String[] names = (1, 2, 3);
▢ String names = new String(“3”);
Select the answer which describes the output of the following code.
int[] ages = {2, 37, 48, 3, 5};
System.out.println(ages[2] + “ “ + ages[1]);
37 48
48 37
37 2
48 3
2 37
48 37
Given the following loop code, what is the name of the array being iterated over?
for(int age : ages) {
…
}
age
for
ages
int
ages
Given the following loop code, how many iterations will be performed?
String[] numbers = { “2”, “3”, “4”, “5” };
for(String number : numbers) {
}
2
4
5
3
4
What is the break keyword used for?
Skip to the end of an if/else block
Raise an exception from within a loop
Skip to the end of a for loop
Skip the current iteration of a loop an proceed to the next
Skip to the end of a for loop
What are the two possible values of a boolean expression?
True
False
A ______ ________ is a combination of variables, values and operators that evaluate to true or false.
boolean expression
The following are examples of what?
==
!=
<
<=
>
>=
Relational Operators
An ____ is an indexed container that holds a set of values of a single type
array
Each item in an array is called an _______
element
Each element in an array is accessed by it’s _______ _______
numberical index
The index of the first element is __
0 (zero)
All elements in an array have to share the same _____
type
Provide example of int array with 3 indexes
Then assign values to that array
int [] ages = new int[3]
ages[0] = 39;
ages[1] = 42;
ages[2] = 97;
create a string array called names with the 3 values assigned.
int[] scores = { “adrian”, “chris”, “samantha” } ;
How would you access the number 27 within the following array?
int[] ages = {39, 27, 64}
ages[1]
How would you change the name of Mary to Bill in the following array?
String[] names = {“Steve”, “Bob”, “Mary”, “Will”}
names[2] = “Bill”;
Loops are used in programs to repeat _____ of statements until an expression is ____ or for specific number of times.
Loops are used in programs to repeat blocks** of statements until an expression is **false or for specific number of times.
In a for loop each iteration returns the ____ ______ of the array.
for (String name : names) {
System.out.println(“Name is “ + name);
}
In a for loop each iteration returns the next element of the array.
In the following for loop what happens during the break?
int passmark = 12;
boolean passed = false;
int[] scores = {4,6,2,8,12,35,9);
for (int unitScore : scores){
if (unitScore >= 12) {
passed = true;
break;
}
}
System.out.println(“At least one passed? “ + passed);
The loop is stopped and it will go the end of the body of the loop and will print out “At least on passed? (score)”
Create a for loop that prints out each name in the string array so that each line appears as “Name is Bob”
String[] names = {“Steve”, “Mary”, “William”}
for (String name : names) {
System.out.println(“Name is “ + name)
}
What happens when continue is used in a loop?
The loop will perform an action, if you set one and then continue iterating over the loop.