Greenfoot Ch 5 Quiz

Reviewed by Editorial Team
The ProProfs editorial team is comprised of experienced subject matter experts. They've collectively created over 10,000 quizzes and lessons, serving over 100 million users. Our team includes in-house content moderators and subject matter experts, as well as a global network of rigorously trained contributors. All adhere to our comprehensive editorial guidelines, ensuring the delivery of high-quality content.
Learn about Our Editorial Process
| By Tcarteronw
T
Tcarteronw
Community Contributor
Quizzes Created: 38 | Total Attempts: 31,980
| Attempts: 308 | Sp�rsm�l: 26
Please wait...

Question 1 / 26
0 %
0/100
Score 0/100
1. Match the following escape sequences.
Submit
Please wait...
About This Quiz
Greenfoot Ch 5 Quiz - Quiz

2. Write a line of code to change the element at index 3 to 5 in the scores array.

Explanation

The correct answer is scores[3] = 5; This line of code will change the element at index 3 in the scores array to 5.

Submit
3. Match the following vocabulary with the definitions.
Submit
4. Variable Declarations need a data type and a variable name

Explanation

Variable declarations in programming languages typically require specifying both a data type and a variable name. The data type determines the kind of values that can be stored in the variable, such as integers, strings, or booleans. The variable name is used to refer to the variable in the code. By providing both a data type and a variable name, the programmer can create a named storage location with a specific type to store and manipulate data in their program. Therefore, the statement "Variable Declarations need a data type and a variable name" is true.

Submit
5. Match the following boolean symbol with its meaning.
Submit
6. Declare a variable that will hold the number of students in a course

Explanation

The correct answer is "int numStudents;" because when declaring a variable to hold the number of students in a course, it is common to use an integer data type (int) as it can store whole numbers. The variable name "numStudents" is also appropriate and descriptive. The other options, "char numStudents;" and "String Students;", are not suitable for this purpose as they are used to store single characters and sequences of characters respectively.

Submit
7. Given the following variable declarations, evaluate the following Boolean expression: int a= 7; int b = 12; int c = 12; int d = 7; (a == b && b==c) 

Explanation

The expression (a == b && b == c) evaluates to false because the first condition (a == b) is false. Since a is 7 and b is 12, they are not equal. Therefore, the overall expression is false.

Submit
8. Declare a variable that will hold either true or false if a name is found in a file

Explanation

The correct answer is boolean nameFound; because a boolean data type can hold either true or false values, which is suitable for storing the result of whether a name is found in a file or not. The other options, String nameFound; and char nameFound; are incorrect because they cannot represent true or false values directly.

Submit
9. Given the following variable declarations, evaluate the following Boolean expression: int a= 7; int b = 12; int c = 12; int d = 7; (a ==c ||a ==b)

Explanation

The given Boolean expression (a == c || a == b) evaluates to False because the first condition (a == c) is False since a is not equal to c (7 is not equal to 12). Therefore, the overall expression is False.

Submit
10. You can initialize an array when it is created. So, for example, the following code would result in an array being correctly declared and intialized. int[] testScores = {100, 95, 60, 75, 85, 55};

Explanation

The statement is true because in the given code, an array named "testScores" is declared and initialized with the values {100, 95, 60, 75, 85, 55} using the curly braces syntax. This is a valid way to initialize an array when it is created.

Submit
11. Declare a variable that will hold someone's middle initial

Explanation

The correct answer is "String middleInitial;, char middleInitial;". This is because the question asks for a variable that will hold someone's middle initial. The middle initial can be a single character, so it can be stored as a char data type. However, it can also be a string if the person has more than one initial or if the middle initial is represented by more than one character. Therefore, both options are correct.

Submit
12. Which of the following is not a data type in Java?

Explanation

The data types in Java are used to define the type of data that a variable can hold. The options provided are int, double, boolean, String, and constant. However, "constant" is not a data type in Java. In Java, constants are typically declared using the "final" keyword and can hold values of any data type, but "constant" itself is not a data type.

Submit
13. Practice with logical operators Given: int age1 = 21; int age2 = 14; int age3 = 15; int birth = 0; String name = "JCCC"; age1 < age2 && age2 < age3

Explanation

age1 (21) F && T
F

Submit
14. Mark the arrays that are declared correctly by good programming standard.

Explanation

The arrays "int[] scores;" and "float[] rates;" are declared correctly by good programming standards. In Java, it is recommended to declare arrays using the square brackets after the data type, so "int[] scores;" and "float[] rates;" follow this convention. On the other hand, "int scores [];" and "float rates [];" are also valid syntax, but they are not considered good programming practice as they can be confusing and make the code less readable.

Submit
15. What is the output of the following code? int limit = 5; int sum = 0;   // Calculate the sum for (int i = 1; i <= limit; i++) {   sum += i; } System.out.println(sum);

Explanation

The given code calculates the sum of numbers from 1 to the value of the variable "limit", which is 5 in this case. It uses a for loop to iterate from 1 to 5 and adds each number to the variable "sum". Therefore, the output of the code is the sum of 1+2+3+4+5, which is 15.

Submit
16. Declare and create a new array of 5 elements for a scores array.  

Explanation

The correct answer is "int[] scores = new int[5];". This code declares and creates a new array called "scores" with 5 elements. The "int[]" indicates that it is an array of integers, and "[5]" specifies the size of the array. This means that the "scores" array can store 5 integer values.

Submit
17. Given the following declaration, answer the questions below:   private String[] words = { "Christmas", "Hello", "TSAS", "Java", "Spring Break" };   What is the value of words[2]?

Explanation

The value of words[2] is "TSAS". This is because the array "words" is initialized with the values "Christmas", "Hello", "TSAS", "Java", and "Spring Break", and array indices start from 0. Therefore, words[2] refers to the third element in the array, which is "TSAS".

Submit
18. What are the instance variables in the code below?
public class MathStudent extends Actor {     protected String firstName;     protected String lastName;         public MathStudent()     {         firstName = "";         lastName = "";     }       public void countToFive()     {         int i = 1;         int limit = 5;         while (i <= 5)         {           System.out.println(i);           i = i + 1;         }     } }

Explanation

The instance variables in the code are firstName and lastName. These variables are declared at the beginning of the class and are marked as protected, which means they can be accessed by other classes in the same package or subclasses. They are used to store the first and last name of the MathStudent object.

Submit
19. What the value of x is at the end:        int i=0;      int x=0;             while (i < 4)         {             x = x + i;             i++;         }

Explanation

The value of x at the end of the code is 3. This is because the code initializes x to 0 and then enters a while loop. In each iteration of the loop, the value of i is added to x, and then i is incremented by 1. The loop continues until i is no longer less than 4. Since the loop iterates 4 times (i=0,1,2,3), the value of x is updated to 0+1+2=3. Therefore, the final value of x is 3.

Submit
20. Mark the following identifiers that are valid names to use in Java.

Explanation

Can not start with a digit and cannot have a dot.

Submit
21. An identifier can be named in Java using letters, digits, underscores and the $ sign.  They may not begin with an underscore.

Explanation

they cannot begin with a digit

Submit
22. Given the following code, the output will be: int i = 1; int limit = 3; while (i <= limit) {   System.out.println(i);   i = i + 1; }

Explanation

The given code initializes the variable "i" to 1 and the variable "limit" to 3. Then, it enters a while loop that will execute as long as "i" is less than or equal to "limit". Inside the loop, it prints the current value of "i" and increments it by 1. This process continues until "i" becomes greater than "limit". Therefore, the output will be the numbers 1, 2, and 3, each on a separate line.

Submit
23. Given the code below, what are the local variables?
public class MathStudent extends Actor {     protected String firstName;     protected String lastName;         public MathStudent()     {         firstName = "";         lastName = "";     }       public void countToFive()     {         int i = 1;         int limit = 5;         while (i <= 5)         {           System.out.println(i);           i = i + 1;         }     } }

Explanation

The local variables in the given code are "limit" and "i". These variables are declared inside the "countToFive" method and are used within the while loop to count and print numbers from 1 to 5. The variables "firstName" and "lastName" are instance variables, not local variables, as they are declared at the class level and can be accessed by any method within the class.

Submit
24. What is the output of the following code? int LIMIT = 4; int sum = 0; while (i <= LIMIT) {   sum = sum + i;   i = i + 1; } System.out.println(sum);

Explanation

The given code initializes a variable `LIMIT` as 4 and a variable `sum` as 0. Then, it enters a while loop where it checks if `i` is less than or equal to `LIMIT`. Since the value of `i` is not given, we can assume it is initialized as 0. Inside the loop, it adds the value of `i` to `sum` and increments `i` by 1. This process continues until `i` becomes greater than `LIMIT`. Finally, it prints the value of `sum`. In this case, the loop will execute 4 times (0, 1, 2, 3) and the sum of these numbers is 6. Therefore, the output will be 6.

Submit
25. Which is the best declaration for  a variable that will hold monthly rainfall. It is initialized to 0?

Explanation

The best declaration for a variable that will hold monthly rainfall and is initialized to 0 is "float totalMonthlyRainfall = 0.0 ;". This is because using a float data type allows for decimal values, which may be necessary when representing rainfall measurements. Additionally, initializing the variable to 0 ensures that it starts with a valid value before any rainfall data is recorded.

Submit
26. What is the output of the following code? // Suppose LIMIT = 3 for (int i = 1; i <= LIMIT; i++) {   System.out.print(i); }

Explanation

The code uses a for loop to iterate from 1 to the value of LIMIT, which is 3 in this case. Inside the loop, it prints the value of the loop variable i. Therefore, the output will be the numbers 1, 2, and 3 printed on separate lines.

Submit
View My Results

Quiz Review Timeline (Updated): Mar 21, 2023 +

Our quizzes are rigorously reviewed, monitored and continuously updated by our expert board to maintain accuracy, relevance, and timeliness.

  • Current Version
  • Mar 21, 2023
    Quiz Edited by
    ProProfs Editorial Team
  • May 14, 2013
    Quiz Created by
    Tcarteronw
Cancel
  • All
    All (26)
  • Unanswered
    Unanswered ()
  • Answered
    Answered ()
Match the following escape sequences.
Write a line of code to change the element at index 3 to 5 in the...
Match the following vocabulary with the definitions.
Variable Declarations need a data type and a variable name
Match the following boolean symbol with its meaning.
Declare a variable that will hold the number of students in a course
Given the following variable declarations, evaluate the following...
Declare a variable that will hold either true or false if a name is...
Given the following variable declarations, evaluate the following...
You can initialize an array when it is created....
Declare a variable that will hold someone's middle initial
Which of the following is not a data type in Java?
Practice with logical operators...
Mark the arrays that are declared correctly by good programming...
What is the output of the following code?...
Declare and create a new array of 5 elements for a scores array....
Given the following declaration, answer the questions below:...
What are the instance variables in the code below?...
What the value of x is at the end:...
Mark the following identifiers that are valid names to use in Java.
An identifier can be named in Java using letters, digits, underscores...
Given the following code, the output will be:...
Given the code below, what are the local variables?...
What is the output of the following code?...
Which is the best declaration for  a variable that will hold...
What is the output of the following code?...
Alert!

Advertisement