Java for Android Week 2 Coursera

Module 4 practice quiz on IF statements

course link : https://www.coursera.org/learn/java-for-android

1.
Question 1
What is the truth value of the following expression?

(-8 >= 9) || (14 > 2)

True

False


These are answers for Java for Android Week 2

2.
Question 2
What is the truth value of the following expression?

(5 > 0) && (7 > 0)

True

False


3.
Question 3
What is the truth value of the following expression?

(8 != 7) && (4 > 4)

True

False


4.
Question 4
What is the truth value of the following expression?

!(8 >= 2*3) || (7 >= 11)

True

False


5.
Question 5
What is the output for the following code segment?


int x = 10;
int y = 27;
if (x > y){
x = x + y;
}
Out.print(x)

20

27

37

10

These are answers for Java for Android Week 2


6.
Question 6
What is the output of the following code?


int age = 23;
if (age >= 55){
Out.println("Your cost is discounted by 10%");
}
if (age >= 21){
Out.println("You can purchase a wristband for $10");
}
if (age >= 16){
Out.println("Your cost is discounted by 15%");
}


You can purchase a wristband for $10
Your cost is discounted by 15%
You may enter for free


You can purchase a wristband for $10


Your cost is discounted by 10%
You can purchase a wristband for $10
Your cost is discounted by 15%
You may enter for free


You can purchase a wristband for $10
Your cost is discounted by 15%

These are answers for Java for Android Week 2


7.
Question 7
What is the output of the following code?


int x = -7;
if(x > 0){
Out.println(x + 10);
}
else if (x == 0){
Out.println(x);
}
else{
Out.println(x - 10);

-7

3

3

-7

-17


8.
Question 8
What value of x would produce the desired output seen below?


if (x > 10){
Out.println("Above average");
if (x > 20){
Out.println("Should continue with the program");
else {
Out.println("May want to continue with the program");
}
else{
Out.println("Do not continue, move to another section");
}

Desired output:

Above average
May want to continue with the program

16

23

-4

7

These are answers for Java for Android Week 2


Module 4 quiz on FOR loops

1.
Question 1
Given the following loop:


for (int i = 1; i < 5; i = i+1) {
out.println("Hello world!");
}
How many lines of output will be printed?

0

4

5

6

infinitely many


2.
Question 2
Given the following loop:


for (int i = 5; i >= 0; i--) {
out.println("Hello world!");
}
How many lines of output will be printed?

0

4

5

6

infinitely many

See also  Java for Android Week 1 Coursera

These are answers for Java for Android Module 4 Quiz


3.
Question 3
Given the following loop:


for (int i = 1; i <= 5; i++) {
for (i = 1; i <= 3; i++) {
out.println("Hello world!");
}
}
How many lines of output will be printed?

0

4

5

6

infinitely many


4.
Question 4
Given the following loop:


for (int i = 1; i <= 5; i++) {
for (int j = 1; j < i; j++) {
out.println("Hello world!");
}
}
How many lines of output will be printed?

0

5

10

15

infinitely many


These are answers for Java for Android Module 4

5.
Question 5
Fill in the blank of the following loop so that 10 lines of output are printed:


for (int i = _ ; i <= 15; i++) {
out.println("Hello world!");
6

6.
Question 6
Given the following loop:


for (int i = 1; i <= 5; i++) { for (int j = 1; j > i; j++) {
out.print("*");
}
out.println();
}
How many stars will be printed in total?

0

5

10

15

infinitely many


7.
Question 7
Given the following loop:

for (int i = 1; i <= 8; i++) {
out.print(_ + " ");
}
What expression would you put in the print statement to produce the following sequence as output:
57 46 35 24 13 2 -9 -20

Please specify your answer in the form: ai+b or ai-b, where a & b are replaced by integer constants. For example, a correctly specified answer would look like this: 5i-8 or this: -4i+6

Do not use parentheses or spaces in your answer.
Enter answer here

-11*i+68

8.
Question 8
Fill in the blank of the following loops so that the following pattern is printed:

*********
 *******
  *****
   ***
    *

for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i-1; j++) {
out.print(" ");
}
for (int j = 1; j <= __; j++) {
out.print(""); } out.println(); }

Please specify your answer in the form: ai+b or ai-b, where a & b are replaced by integer constants. For example, a correctly specified answer would look like this: 5i-8 or this: -4*i+6

Do not use parentheses or spaces in your answer.

Answer: -2*i+11

Module 4 practice quiz on indefinite loops

1.
Question 1
Consider the following code:


int i = 17;
int x = 4;
while (i>x){
i = i-3;
out.print(i + " ");
}

What is the output of this code?

14 11 8 5

14 11 8 5 2

The loop would not execute at all.

17 14 11 8 5


These are answers for Java for Android Module 4

2.
Question 2
Consider the code


int i = 23;
int n = 25;
do{
out.print(i + " ");
}while (i < n);
i = i-5;
What will the output of this code be?

The loop would never execute.

See also  Java for Android Week 4 Coursera

23

23 18

The loop would run continuously, repeatedly printing out the number 23.


3.
Question 3
Given the code

int i = 10;
while (i > 0){
out.print(i + " ");
i = i-2;
}
Which for loop would produce the same results?
for (int i = 10; i>0; i=i-2){
out.print(i + " ");
}
for (int i = 0; i>0; i = i+2){
out.print(i + " ");
}
for (int i = 10; i<0; i = i-2){
out.print(i + " ");
}
for (int i = 10; i<0; i++){
out.print(i + " ");


4.
Question 4
Suppose every player in your game is faced with the task of “rolling” a set of dice to determine what tools they can take with them on their next adventure. Some of the outcomes of the roll are positive (earn a “Bonus” tool etc.) while some are negative (lose all currently held tools, etc.). Every player must roll at least once, but may choose to roll as often as they like by purchasing extra rolls. Which loop construct and accompanying explanation best describe a good coding choice?

do-while loop, because a player may choose not to roll at all.

for loop, because each player will roll a predetermined number of times.

do-while loop, because every player must roll at least once.

while loop, because every player must roll at least once.


5.
Question 5
Consider the code

int i = 1;
while (<>){
i = i*n;
}

What should be written in place of <> so that the loop ends when i is at least 34? You may assume n is an int variable that has been declared and given a value prior to this code (the value of n does not affect the answer to this question).

Answer: i<34

These are answers for Java for Android Module 4


Module 5 Quiz

Total points 13

1.

Question 1

Of the following, which is the correct syntax to declare an array of 24 boolean elements named arr?

boolean[24] arr = new boolean[24];

boolean arr[24];

boolean arr[] = new boolean;

boolean[] arr = new boolean[24];

boolean arr = new boolean[24];

2.

Question 2

Given the declaration of the 24 element boolean array arr above, what is the index of the last element of the array?

22

25

23

24

3.

Question 3

If you wanted to initialize the boolean array arr so that all elements contain the value true, you could use the following: arr.fill(true);

TRUE

FALSE

4.

Question 4

Arrays of class type require a two-phase initialization. One phase to create the array and another phase to create the objects in the array.

See also  Java for Android Week 3 Coursera

FALSE

TRUE

5.

Question 5

These are answers for Java for Android Module 5

Given an integer array named arr, the number of elements in the array can be determined by the following method call: arr.length()

TRUE

FALSE

6.

Question 6

Which of the following statements is true (pick only one):

An array can be sized dynamically, and it can be resized without allocating a new array.

An array can be sized dynamically, but it cannot be resized without allocating a new array.

An array cannot be sized dynamically when the program is running.

7.

Question 7

What is the value of the variable s after the following section of code executes?

int s = 0;
int [] a = {12, 23, 34, 45, 56};
for (int i=1; i<a.length; i++) {
s += a[i];
}

0

170

114

158

8.

Question 8

What type of collection would we use if we wanted no duplicates?

Set

Map

Queue

List

9.

Question 9

Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;
list.add("alpha");
list.add("bravo");
list.add("charlie");
list.add("delta");
list.add("echo");

Which of the following will replace the element “charlie” with “foxtrot“?

list.add(“foxtrot”, list.indexOf(“charlie”));

list.set(“foxtrot”,”charlie”);

list.set(list.indexOf(“charlie”),”foxtrot”);

list.set(“charlie”,”foxtrot”);

list[2] = “foxtrot”;

10.

Question 10

Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;
list.add("alpha");
list.add("bravo");
list.add("charlie");
list.add("delta");list.add("echo");

Which of the following will change the list so that it looks like:

alpha
bravo
charlie
delta

list.remove(list.size());

list.remove(list.size()-1);

list.clear(“echo”);

list.remove(5);

list.empty(“echo”);

These are answers for Java for Android Module 5

11.

Question 11

Examine the following code:

ArrayList<String> list = new ArrayList<String>() ;
list.add("alpha");
list.add("bravo");
list.add("charlie");
list.add("delta");
list.add("echo");
for ( ___________ name : ___________ ) {
out.println( ______________ );
}

Fill in the blanks so that all the elements in the ArrayList are printed.

 String   iterator()   next()
 iterator()   hasNext()   next()
int   String   name
String   list   name
 String   list   next()

12.

Question 12

A HashMap can map keys of any type to values of any type.

TRUE

FALSE

13.

Question 13

Which of the following is not a feature/advantage of a HashMap?

HashMaps can use any object type as a key.

HashMaps have fast lookup.

HashMaps keep their keys in sorted order.

HashMaps have fast insert.

These are answers for Java for Android Module 5


Week 1 here https://progies.in/java-for-android-week-1-coursera-quiz-and-assignments-answers

Latest Progies