Java SE 11: Expressions, Arrays, & Loops Flashcards

1
Q

Select the possible values returned by a Boolean expression.

▢ 1

▢ 0

▢ true

▢ 2

▢ false

▢ no

▢ yes

A

1

0

☑ true

2

☑ false

no

yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q
A

C Less than or equal to

A Equal to

B Not equal to

D Greater than or equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

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;

A

largeVenue = (attendees >= 5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

You will have 0 hours remaining.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Which property or method is used to get the number of elements in an array?

sizeof

lengthof

length

size

A

length

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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”);

A

▢ 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”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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

A

48 37

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Given the following loop code, what is the name of the array being iterated over?

for(int age : ages) {

}

age

for

ages

int

A

ages

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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

A

4

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

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

A

Skip to the end of a for loop

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the two possible values of a boolean expression?

A

True

False

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

A ______ ________ is a combination of variables, values and operators that evaluate to true or false.

A

boolean expression

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

The following are examples of what?

==

!=

<

<=

>

>=

A

Relational Operators

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

An ____ is an indexed container that holds a set of values of a single type

A

array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Each item in an array is called an _______

A

element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Each element in an array is accessed by it’s _______ _______

A

numberical index

17
Q

The index of the first element is __

A

0 (zero)

18
Q

All elements in an array have to share the same _____

A

type

19
Q

Provide example of int array with 3 indexes

Then assign values to that array

A

int [] ages = new int[3]

ages[0] = 39;

ages[1] = 42;

ages[2] = 97;

20
Q

create a string array called names with the 3 values assigned.

A

int[] scores = { “adrian”, “chris”, “samantha” } ;

21
Q

How would you access the number 27 within the following array?

int[] ages = {39, 27, 64}

A

ages[1]

22
Q

How would you change the name of Mary to Bill in the following array?

String[] names = {“Steve”, “Bob”, “Mary”, “Will”}

A

names[2] = “Bill”;

23
Q

Loops are used in programs to repeat _____ of statements until an expression is ____ or for specific number of times.

A

Loops are used in programs to repeat blocks** of statements until an expression is **false or for specific number of times.

24
Q

In a for loop each iteration returns the ____ ______ of the array.

for (String name : names) {

System.out.println(“Name is “ + name);

}

A

In a for loop each iteration returns the next element of the array.

25
Q

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);

A

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)”

26
Q

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”}

A

for (String name : names) {

System.out.println(“Name is “ + name)

}

27
Q

What happens when continue is used in a loop?

A

The loop will perform an action, if you set one and then continue iterating over the loop.