Greenfoot Ch 5 Quiz

26 Questions | Total Attempts: 172

SettingsSettingsSettings
Please wait...
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

  • 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

  • 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

  • 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

  • 5. 
    Variable Declarations need a data type and a variable name
    • A. 

      True

    • B. 

      False

  • 6. 
    Which of the following is not a data type in Java?
    • A. 

      Int

    • B. 

      Double

    • C. 

      Boolean

    • D. 

      String

    • E. 

      Constant

  • 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

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

      7days

    • B. 

      _days

    • C. 

      NumDays

    • D. 

      $days

    • E. 

      Num.days

  • 9. 
    Declare a variable that will hold the number of students in a course
    • A. 

      Int numStudents;

    • B. 

      Char numStudents;

    • C. 

      String Students;

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

  • 11. 
    Declare a variable that will hold someone’s middle initial
    • A. 

      String middleInitial;

    • B. 

      Char middleInitial;

    • C. 

      Int middleInitial;

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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

  • 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

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

      Int[] scores;

    • B. 

      Int scores [];

    • C. 

      Float[] rates;

    • D. 

      Float rates [];

  • 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];

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

  • 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

Back to Top Back to top