Math and Random
Name That Output
Class Design and OOP (Book Class)
Arrays
Potpourri
100

The value of result after executing the following statement.


int result = (int) Math.pow(3, 2);

What is 9?

100

This line of code will print the traditional Computer Science greeting of "Hello world!"

What is 

System.out.println("Hello World!");

100

public class Book
{
   private int numPages;
   // Some code omitted from this class

   public void setNumPages(int pages)
   {
        numPages = pages;
   }

   public int getNumPages()
   {
        return numPages;
   }
}

The name of the instance variable given in the Book class.

What is numPages?

100

It is the result of the code segment below.

String[] weekdays = {"Mon", "Tue", "Wed", "Thurs", “Fri”};
System.out.println(weekdays.length);
System.out.println(weekdays[1]);
System.out.println(weekdays[5]);

What is an Array Index Out of Bounds Exception?

100

It is the value of variable val after the following code segment has executed.

int num1 = 7, num2 = 1, num3 = 5, x1 = 2, x2 = 8, val = 0;

if (x1 < x2)
{
   if (num1 != num2)
      val++;
   else
      val--;
}
else
   val = x1 + x2;

What is 1?

200

The line of Java code that will correctly calculate the square root of 45 and store it in a double variable called num.

What is

double num = Math.sqrt(45);

200

The output of the following code segment.

boolean coldTemps = false;
int month = 4;

if (coldTemps && (month < 3))
   System.out.println("Winter");
else if (month == 3 || month == 4)
   System.out.println("Spring-ish");
else if (!coldTemps && month > 4)
   System.out.println("Summer");
else
   System.out.println("Fall");

What is Spring-ish?

200

public class Book
{
   private int numPages;
   // Some code omitted from this class

   public void setNumPages(int pages)
   {
        numPages = pages;
   }

   public int getNumPages()
   {
        return numPages;
   }
}

The name of the accessor method in the Book class.

What is getNumPages?

200

It is the line of code that will create an array (named letters) of 5 Strings and initialize them to the letters “A” - ”E” (in order) using an initializer list.  

What is 

String [] letters = {"A", "B", "C", "D", "E"};

200

It is the location (x and y) of the shape drawn in the following line of code:

grph.drawRect(30, 40, 80, 100);

What is (30, 40)?

300

The range of possible values that could be assigned to num in the code segment below.


int num = (int) (Math.random() * 20 - 10);

What is -10 to 10?
300

The output of the following code segment?

int x = 0;
for (int index = 3; index < 12; index += 3)
{
   x += index;
}
System.out.println("x is :  " + x);

What is "x is 18"?

300

public class Book
{
   private int numPages;
   // Some code omitted from this class

   public void setNumPages(int pages)
   {
        numPages = pages;
   }

   public int getNumPages()
   {
        return numPages;
   }
}

The name of the mutator (modifier) method in the Book class.

What is setNumPages?

300

It is a code segment that will correctly declare and instantiate an array (named strArray) of 20 Strings.  It does not need to assign any values to the specific Strings.

What is
String [] strArray = new String[20];

300

It is the name given to the following boolean expression where only the first condition is checked.

int x=10;
int y = 0;

if(x==y && y < 20)

What is a short circuit?

400

This Java statement will generate a random integer in the [ 5..15 ] range.

What is

int val = (int) (Math.random() * 11 + 5);

400

The output of the following code segment.

int index = 1, x = 0;

while (x <= 6)
{
   index++;
   x += index;
}
System.out.println(x);

What is 9?

400

public class Book
{
   private int numPages;
   // Some code omitted from this class

   public void setNumPages(int pages)
   {
        numPages = pages;
   }

   public int getNumPages()
   {
        return numPages;
   }
}

This line of code will declare and instantiate a Book object named b using a default constructor (no parameters).

What is

Book b = new Book();

400

It is the missing code below that will allow the loop to count how many array elements are multiples of 5.   


for(int i = 0; i < vals.length; i++)
{
       /* MISSING CODE */
      count++;
}

What is

if(vals[i] % 5 == 0)

400

Assume that grph is a graphics object. This line of code will produce a circle with a diameter of 150 located at the pixel (100, 20)?

What is 

grph.drawOval(100,20,150,150);

500

This line of code will print the absolute value of a variable named var.

What is
System.out.println(Math.abs(var));

500

The result of the following code segment.

for (int i = 1; i < 20; i *= 2)
{
    int count = count + 1;
}
System.out.print(“count is “ + count);

What is a syntax error?

500

public class Book
{
   private int numPages;
   // Some code omitted from this class

   public void setNumPages(int pages)
   {
        numPages = pages;
   }

   public int getNumPages()
   {
        return numPages;
   }
}

It's how you print out the number of pages in a Book object called b from a main method class.  You may assume the object b has already been declared and instantiated correctly.

What is

System.out.println(b.getNumPages());

500

It is a for loop that will successfully increment every element in an array named vals.

What is 

for(int i=0; i < vals.length; i++)
    vals[i]++;

500

It is the line of code that would compare if the following two Strings named name1 and name2 are storing the same name.


What is 

if(name1.equals(name2))