Greenfoot Ch 5 Quiz

Approved & Edited by ProProfs Editorial Team
The editorial team at ProProfs Quizzes consists of a select group of subject experts, trivia writers, and quiz masters who have authored over 10,000 quizzes taken by more than 100 million users. This team includes our in-house seasoned quiz moderators and subject matter experts. Our editorial experts, spread across the world, are rigorously trained using our comprehensive guidelines to ensure that you receive the highest quality quizzes.
Learn about Our Editorial Process
| By Tcarteronw
T
Tcarteronw
Community Contributor
Quizzes Created: 38 | Total Attempts: 29,972
Questions: 26 | Attempts: 272

SettingsSettingsSettings
Greenfoot Quizzes & Trivia

Questions and Answers
  • 1. 

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

    • A.

      1

    • B.

      2

    • C.

      3

    • D.

      4

    Correct Answer
    C. 3
    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.

    Rate this question:

  • 2. 

    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)

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    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.

    Rate this question:

  • 3. 

    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) 

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    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.

    Rate this question:

  • 4. 

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

    • A.

      Christmas

    • B.

      Hello

    • C.

      TSAS

    • D.

      Java

    Correct Answer
    C. TSAS
    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".

    Rate this question:

  • 5. 

    Variable Declarations need a data type and a variable name

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

  • 6. 

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

    • A.

      Int

    • B.

      Double

    • C.

      Boolean

    • D.

      String

    • E.

      Constant

    Correct Answer
    E. Constant
    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.

    Rate this question:

  • 7. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    they cannot begin with a digit

    Rate this question:

  • 8. 

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

    • A.

      7days

    • B.

      _days

    • C.

      NumDays

    • D.

      $days

    • E.

      Num.days

    Correct Answer(s)
    B. _days
    C. NumDays
    D. $days
    Explanation
    Can not start with a digit and cannot have a dot.

    Rate this question:

  • 9. 

    Declare a variable that will hold the number of students in a course

    • A.

      Int numStudents;

    • B.

      Char numStudents;

    • C.

      String Students;

    Correct Answer
    A. Int numStudents;
    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.

    Rate this question:

  • 10. 

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

    • A.

      Boolean nameFound;

    • B.

      String nameFound;

    • C.

      Char nameFound;

    Correct Answer
    A. Boolean nameFound;
    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.

    Rate this question:

  • 11. 

    Declare a variable that will hold someone’s middle initial

    • A.

      String middleInitial;

    • B.

      Char middleInitial;

    • C.

      Int middleInitial;

    Correct Answer(s)
    A. String middleInitial;
    B. Char middleInitial;
    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.

    Rate this question:

  • 12. 

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

    • A.

      Float totalMonthlyRainfall = 0.0 ;

    • B.

      Int totalMonthlyRainfall = 0;

    • C.

      Float totalMonthlyRainfall;

    • D.

      Int totalMonthlyRainfall;

    Correct Answer
    A. Float totalMonthlyRainfall = 0.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.

    Rate this question:

  • 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

    • A.

      True

    • B.

      False

    Correct Answer
    B. False
    Explanation
    age1 < age2 && age2 < age3
    (21) < (14) && (14) < (15)
    F && T
    F

    Rate this question:

  • 14. 

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

    • A.

      1 2 3

    • B.

      1 2 3 4

    • C.

      1 2 3

    • D.

      1 2 3 4

    Correct Answer
    A. 1 2 3
    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.

    Rate this question:

  • 15. 

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

    • A.

      FirstName, lastName

    • B.

      Limit, i

    • C.

      FirstName, lastName, limit, i

    Correct Answer
    A. FirstName, lastName
    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.

    Rate this question:

  • 16. 

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

    • A.

      FirstName, lastName

    • B.

      Limit, i

    • C.

      FirstName, lastName, limit, i

    Correct Answer
    B. Limit, i
    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.

    Rate this question:

  • 17. 

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

    • A.

      10

    • B.

      9

    • C.

      11

    • D.

      8

    Correct Answer
    A. 10
    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.

    Rate this question:

  • 18. 

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

    • A.

      0123 2 3

    • B.

      123 1 2 3

    • C.

      1 2 3

    • D.

      0 1 2 3

    Correct Answer
    B. 123 1 2 3
    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.

    Rate this question:

  • 19. 

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

    • A.

      14

    • B.

      15

    • C.

      16

    • D.

      17

    Correct Answer
    B. 15
    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.

    Rate this question:

  • 20. 

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

    • A.

      Int[] scores;

    • B.

      Int scores [];

    • C.

      Float[] rates;

    • D.

      Float rates [];

    Correct Answer(s)
    A. Int[] scores;
    C. Float[] rates;
    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.

    Rate this question:

  • 21. 

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

    • A.

      Int[] scores = new int[5];

    • B.

      Int[5] scores = new int[];

    • C.

      Int scores = new int[5];

    Correct Answer
    A. Int[] scores = new int[5];
    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.

    Rate this question:

  • 22. 

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

    • A.

      Scores[3] = 5;

    • B.

      Scores[5] = 0;

    • C.

      Scores[5] = 5;

    Correct Answer
    A. Scores[3] = 5;
    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.

    Rate this question:

  • 23. 

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

    • A.

      True

    • B.

      False

    Correct Answer
    A. True
    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.

    Rate this question:

Quiz Review Timeline +

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
Back to Top Back to top
Advertisement
×

Wait!
Here's an interesting quiz for you.

We have other quizzes matching your interest.