Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Quiz 2 (Attempt-2)

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

Quiz 2 - Modules 5 and 6


Due 26 Apr at 17:00
Points 30
Questions 30
Available 22 Apr at 9:00 - 26 Apr at 17:00
Time limit 35 Minutes
Allowed attempts 2

Instructions
You have 35 minutes to complete this quiz from the time that you begin.

You will have 2 attempts, with the best score being kept.

Attempt history
Attempt Time Score
KEPT Attempt 2 18 minutes 27.5 out of 30

LATEST Attempt 2 18 minutes 27.5 out of 30

Attempt 1 33 minutes 27 out of 30

Score for this attempt: 27.5 out of 30


Submitted 24 Apr at 23:17
This attempt took 18 minutes.

Question 1
1 / 1 pts
Which of these statements about a Java class is not true?
Classes act as blueprints for objects.

Each class can be used as often as needed to create objects.

Correct!
Each class must have one and only one constructor.

A class is a list of declarations of attributes, constructors, and methods.


Question 2
1 / 1 pts

For the following code which statement is not true?

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 1/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

public class Circle {


private double radius;
public double x;
private double y;
private double z;
}

radius is not available to code written outside the Circle class

Correct!
z is available to code written outside the Circle class

radius, x, y and z are called members of the Circle class

x is available to code that is written outside the Circle class


Question 3
1 / 1 pts
The relationship between a class and an object is best described as
objects and classes are the same thing
Correct!
objects are instances of classes

classes are instances of objects

classes are programs while objects are variables


Question 4
0 / 1 pts

What output will be produced when an instance of Test is created?

class Test {
Test() {
g();
}
int g() {
System.out.println("Hi from g");
int h() {
System.out.println("Hi from h");
}
}
}

Correct answer
Compiling errors; no output

Hi from g

Run-time errors; no output


You Answered
https://canvas.flinders.edu.au/courses/18866/quizzes/122359 2/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

Hi from g Hi from h


Question 5
0.5 / 1 pts

Which of the following statements are true?

(You can select multiple answers)

Correct!
All variables must be given a type when they are declared.

The arithmetic operators *, /, \% and + all have the same level of precedence.
Correct!
The number of arguments in the method call must match the number of parameters in the method declaration.

You Answered

A local variable is declared in the body of a method and can be used anywhere within the class containing the method.


Question 6
1 / 1 pts
Which of these statements best describes a Java method?
It defines a blueprint from which objects are constructed
Correct!
It defines an action that an object can perform

It specifies how an object is initialised

It contains a value that defines part of an object's state


Question 7
1 / 1 pts
Which of these statements about Java methods is not true?
The Java system provides standard (predefined) methods.

Correct!
Definition of a method may be included in the body of another method.

When a return statement executes in a user-defined method, the method immediately exits.

There are two types of user-defined methods: value-returning methods and void methods.

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 3/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

Question 8
1 / 1 pts
It is common practice in object-oriented programming to make all of a class' ...
Correct!
attributes private

attributes public

attribute and methods public

methods private


Question 9
1 / 1 pts
Which of these items is not something that you might find in a constructor?
A statement that assigns a newly created object to an instance variable.

A call to a public method of another object.

Correct!
The definition of a new method.

A call to the superclass constructor.


Question 10
1 / 1 pts
A special method that is invoked to set up an object during instantiation is called a _________________
new method

creator

main method

Correct!
constructor


Question 11
1 / 1 pts
____________ are defined when a method is declared, while ____________ are defined when the
method is invoked
local variables; instance variables

instance variables; local variables

actual parameters; formal parameters

Correct!

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 4/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

formal parameters; actual parameters


Question 12
1 / 1 pts
The behavior of an object is defined by the object's
instance data

constructor

all answers are correct

Correct!
methods

visibility modifiers


Question 13
1 / 1 pts
Which of the following patterns should be used when instantiating a DecimalFormat object, if the
intended output format was 3.810?
Correct!
"0.000"

"0.0##"

"#.0"

"0.#"

"#.###"


Question 14
0 / 1 pts
Consider two methods, one private and the other public. Which statement does not apply to both
methods?
Both methods can have parameters and can return a value.

You Answered
Both methods can access private attributes of the class.

Both methods can use local variables to store temporary values.

Correct answer
Both methods can be called by methods of other classes.

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 5/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

Question 15
1 / 1 pts
If you were examining the source code for a class, how would you distinguish methods from
constructors?
Correct!
Method names are user defined; constructors have the same name as the class.

Methods are private; constructors are public or protected.

Methods contain local variables; constructors contain instance variables.

Methods return values; constructors do not.


Question 16
1 / 1 pts

What will be printed after the following code is executed?

for(int number = 5; number <= 15; number += 3)


System.out.print(number + ", ");

This is an invalid for statement

Correct!
5, 8, 11, 14,

5, 8, 11, 14, 17,

5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,


Question 17
1 / 1 pts

Given that s is a String, what does the following loop do?

for (int j = s.length(); j > 0; j--)


System.out.print(s.charAt(j-1));

Correct!
it prints s out backwards

it yields a run-time error because there is no character at s.charAt(j-1) for j = 0

it prints s out backwards after skipping the last character

it prints s out forwards

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 6/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

it prints s out backwards but does not print the 0th character


Question 18
1 / 1 pts

What would be the value of x after the following statements were executed?

int x = 10;
switch(x) {
case 10:
x += 15;
case 12:
x -= 5;
break;
default:
x *= 30;
}

25
Correct!
20

30


Question 19
1 / 1 pts

How many times will the following for loop be executed?

for(int count = 10; count <= 21; count++)


System.out.println("Java is Great!!!");

11

0
Correct!
12

10


Question 20
1 / 1 pts

How many times will the following loop iterate?

int x = 10;
do {

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 7/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024
System.out.println(x);
x--;
} while (x > 0);

9 times

0 times

Correct!
10 times

11 times

1 time


Question 21
1 / 1 pts

What should the loop control expressions be in order to make this code print the numbers counting
backwards from 20 to 10?

for (<< loop control >>) {


System.out.println(i);
}

Correct!
int i = 20; i >= 10; i--;

int i = 10; i <= 20; i--;

int i = 20, 10, --1;

int i <= 20; i >= 10; i--;


Question 22
1 / 1 pts

What is the exact range of values you could use for number to terminate the following while loop?

while (number < 100 || number > 500) {


System.out.println("hi");
}

Correct!
numbers in the range 100-500 (ends included)

numbers greater than 500

numbers less than 100

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 8/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

numbers in the range 100-499 (ends included)


Question 23
1 / 1 pts
The ____________ is ideal in situations where the exact number of iterations is known.
Correct!
for loop

if statement

while loop

do loop


Question 24
1 / 1 pts

What should the << loop control >> expression be in order to make this code count backwards from 100
by threes?

for (<< loop control>>) {


System.out.println(i);
}

int i = 100; i - 3; i--


Correct!
int i = 100; i > 0; i = i - 3

int i = 100; i %3 == 0; i--

int i = 100, 1, --3


Question 25
1 / 1 pts

What will be the value of x after the following code is executed?

int x = 10;
for(int y = 5; y < 20; y += 5)
x += y;

Correct!
40

30

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 9/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024

25

Invalid __for} statement


Question 26
1 / 1 pts

Predict the output of the following nested loops:

for(int i = 1; i < 4; i++) {


for(int k = 1; k > 0; k++) {
System.out.println(i + " times " + k + " = " + i * k);
}
}

1 times 1 = 1
1 times 2 = 2
1 times 3 = 3
1 times 1 = 1
1 times 2 = 2
1 times 3 = 3

Correct!
infinite loop

1 times 1 = 1
1 times 2 = 2
1 times 3 = 3

1 times 1 = 1
1 times 2 = 2
1 times 3 = 3
1 times 1 = 1
2 times 1 = 2
3 times 1 = 3


Question 27
1 / 1 pts
Choose the best word to fill in the blank in the statement: "The do ... while statement tests the loop-
continuation condition ___________ executing the loop's body ".
before

when
Correct!
after

always
https://canvas.flinders.edu.au/courses/18866/quizzes/122359 10/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024


Question 28
1 / 1 pts

How many times will the following do loop be executed?

int x = 11;
do {
x += 20;
} while (x > 100);

Correct!
1


Question 29
1 / 1 pts

What will this code print?

int x = 1;
for (int y = 1; y <= 10; y++) {
if (y > 7) x++;
}
System.out.println(x);

2
Correct!
4


Question 30
1 / 1 pts

What is printed by this code?

int x = 0;
for (int y = 1; y <= 10; y++) {
if (y % 2 != 0)
x++;

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 11/12
4/24/24, 11:17 PM Quiz 2 - Modules 5 and 6: Computer Programming 1 GE | COMP8702 ONC-U + ONC-FP S1 2024
}
System.out.println(x);

Correct!
5

10

Quiz score: 27.5 out of 30

https://canvas.flinders.edu.au/courses/18866/quizzes/122359 12/12

You might also like