Iteration
Booleans and Conditionals
Arrays
2D Arrays
!!OBJECTS!!
100

What are the first and last values output by the following code segment?

int t = 13; 

while (t < 29)

{

System.out.println(t);

t++; 

}

13, 28

100

What does the following code print when x has been set to .8?

if (x < .25)
{
    System.out.println("first quartile");
}
else if (x < .5)
{
    System.out.println("second quartile");
}
else if (x < .75)
{
    System.out.println("third quartile");
}
else
{
    System.out.println("fourth quartile");
}

fourth quartile

100

What is the output of the following code segment?

int[ ] numbers = {44, 33, 22, 11};
for (int num : numbers)
{
    num *= 2;
}
for (int num : numbers)
{
    System.out.print(num + " ");
}

44 33 22 11

100

What would set the value for the 3rd row and 2nd column of a 2D array called chickens to 5?

chickens[2][1] = 5;

100

What is NOT a primitive data type?

String

200

Assume that nums has been created as an ArrayList object and initially contains the following Integer values: [0, 0, 4, 2, 5, 0, 3, 0]. What will nums contain as a result of executing the following method numQuest?

private List<Integer> nums;

//precondition: nums.size() > 0
//nums contains Integer objects
public void numQuest() {
   int k = 0;
   Integer zero = new Integer(0);
   while (k < nums.size())
   {
      if (nums.get(k).equals(zero))
         nums.remove(k);
      k++;
   }
}

 [0, 4, 2, 5, 3]

200

 Consider the following code segment. What is printed as a result of executing the code segment?

int x = 3;
if (x > 2)
{
    x = x * 2;
}
if (x > 4)
{
   x = 0;
}
System.out.print(x);

0

200

What correctly initializes an array arr to contain four elements each with value 0?

I. int[] arr = {0, 0, 0, 0};

II. int[] arr = new int[4];

III. int arr[] = new arr[4];

for(int i = 0; arr < arr.length; i ++){

arr[i] = 0;}

| , ||, & |||

200

Consider the following code segment. What is the value of sum as a result of executing the code segment?

int[][] frog = { {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12} };
int sum = 0;
for (int[] x : frog)
{
    for (int y = 0; y < x.length - 1; y++)
    {
         sum += x[y];
    }
}

54

200

int hog = 6;

int potato = 7;

int pig = 2;

System.out.println((hog % pig) * potato);

What does this code print?

BONUS 500 POINTS: What is the difference between a hog and a pig?

0

A hog is over 120 lbs and a pig is under 120 lbs.

300

Consider the following method!

public static void mystery(List nums) 

{ for(int k = 0; k < nums.size(); k++){

if(nums.get(k).intValue() == 0){

nums.remove(k);}}}

Assume that a List values initially contains the following Integer values. [0, 0, 4, 2, 5, 0, 3, 0] 

What will values contain as a result of executing mystery(values) ?

[0, 4, 2, 5, 3]

300

Consider the following statement.

boolean alligator = (5 % 3 == 0) == (3 > 5);

What is the value of alligator after the statement has been executed?

true

300

Given that array is an array of integers and target is an integer value, what best describes the conditions under what code segment will return false?

public boolean find(int[] array, int target)
{
    for (int val : array)
    {
        if (val == target)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    return false;
}

Whenever the first element in array is not equal to target.

300

What is the value of total after the following code executes?

int[][] grain = { {4, 5, 6, 7}, {0, 1, 2, 3}, {3, 2, 1, 0}, {8, 9, 1, 2}}; 

int total = 0; 

for (int row = 0; row < grain.length; row++) { 

total = total + grain[row][1];

}

17

300

 What are the data or properties of an object called?

attributes

400

Given the following code, describe the condition needed for temp to be true when it is returned?

boolean temp = false; 

int count = 0; 

for ( int testVal : a)

{

if ( testVal == val ) count++; 

temp = count > 1; 

return temp;

Whenever more than 1 element in a is equal to val

400

What is true after the code executes?

String s1 = new String("hi");
String s2 = new String("bye");
String s3 = new String("hi");
s2 = s1;

A. s1 == s2 && s1 == s3
B. s1 == s2 && s1.equals(s3)
C. s1 != s2 && s1.equals(s3)

B

400

Consider the following method. Given an array initialized to {4, 10, 15}, what represents the contents of the array after a call to mystery(array, 2)?

public void mystery(int[] a, int i)
{
   a[i] = a[i-1] * 2;
}

 [4, 10, 20]

400

Write a loop that will traverse through the 2D array "party" and print out all of the values.

int[][] array = { {1,2,3},{4,5,6}};


for (int row = 0; row < party.length; row++)
{
    for (int col = 0; col < party[0].length; col++)
    {
         System.out.println( party[row][col] );
    }
 }

400

Consider the following proposed constructors for this class. 

I. public NamedPoint() { name = ""; } 

II. public NamedPoint(int d1, int d2, String pointName) { x = d1; y = d2; name = pointName; }

 III. public NamedPoint(int d1, int d2, String pointName) { super(d1, d2); name = pointName; } 

Which constructor would be legal for the NamedPoint class? (there's two options)

I and III only

500

Assume that list has been initialized with the following Integer objects: [9, 3, 17, 2, 16, 4, 1]. What shows the values in list after a call of mystery(4)?

private List list<Integer>;

public void mystery(int n) { 

for (int i= 0; i < n; i++) { 

Object obj = list.remove(0); 

list.add((Integer)obj); 

}

[16, 4, 1, 9, 3, 17, 2]

500

DEMORGANS LAW!!

What is printed when the following code executes and x equals 4 and y equals 3?

int x = 4, y = 3;
if (!(x < 3 || y > 2))
{
   System.out.println("first case");
}
else
{
   System.out.println("second case");
}

second case

500

SURPRISE! This is actually over an ArrayList YAY!

What will print when the following code executes?

List<Integer> potato = new ArrayList<Integer>();
potato.add(new Integer(1));
potato.add(new Integer(2));
potato.add(new Integer(3));
potato.set(2,new Integer(4));
potato.add(1, new Integer(5));
potato.add(new Integer(6));
System.out.println(potato);

extra 100 points if you state the difference between an array and an ArrayList!

 [1, 5, 2, 4, 6] 

Once an array is created, its size cannot be changed, but an ArrayLists size can always be changed. Arrays are type-specific, meaning all elements must be of the same data type (e.g., all integers, all strings). Arrays use square brackets for declaration and access while ArrayLists use these {}.

500

Consider the following code segment. What is the last row of numbers printed when this code segment is executed?

 int[][] points = { {11, 12, 13, 14, 15},
                    {21, 22, 23, 24, 25},
                    {31, 32, 33, 34, 35},
                    {41, 42, 43, 44, 45}};
 for (int row = 0; row < points.length; row++)
 {
     for (int col = points[0].length - 1; col >= row; col--)
     {
          System.out.print(points[row][col] + " ");
     }
     System.out.println();
}

BONUS 100 POINTS: What year did Coach McDonald graduate high school?

45 44

!!2000!!

500

A student has created a Party class. The class contains variables to represent the following.

  • An int variable called numOfPeople to represent the number of people at the party.

  • A boolean variable called discoLightsOn to represent whether the disco ball is on.

  • A boolean variable called partyStarted to represent whether the party has started.

The object myParty is declared as type Party. Which of the following descriptions is accurate?

A. boolean is an attribute of the myParty object.

B. myParty is an attribute of the Party class.

C. myParty is an instance of the Party class.

D. myParty is an attribute of the Party instance.

E. numOfPeople is an instance of the Party object. 

C!!!!!

M
e
n
u