Array/ArrayList
Strings
Classes/Interface/Abstract
Java Language
Miscellaneous
100

How would you initialize an int array with 10 elements?

int[] arr = new int[10];

100

String str = ”string”;


Create an int with the length of string str.

int len = str.length();

100

Answer the statement as true or false:

An abstract object may be directly instantiated.

False

100

The private access modifier in java does what?

Private means it is only accessible in the class it is initialized in

100

What sort would be most efficient for sorting an array of 1 million elements?

Merge Sort

200

How would you add the word “java” to ArrayList list after its 3rd element?

ArrayList<String> list = new Arraylist<String>();

list.add(“a”);

list.add(“b”);

list.add(”c”);

list.add(“d”);

list.add(3, “java”);

200

String a = “java”;

String b = “language”;

String c = a+b;

System.out.println(c);

What is printed out?

javalanguage

200

Answer the statement as true or false:

An interface object may be directly instantiated.

False

200

The public access modifier in java does what?

Public means it is accessible from anywhere

200

 public int mystery(int m)

 {

   if (m == 1)

   { 

       return 3;

   }

   else 

   { 

       return 3 * mystery(m - 1);   

    }

  }

What does mystery(5) return?

243

300

What is printed out?  

int num = 0;

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

for(int j=0; j<5; j++){

num++;

}

}

System.out.print(num);

15

300

String a = “apple”;

String b = “pie”;

String c = a.substring(1, 4) + b.substring(1);

System.out.println(c);

What is printed out?

ppleie

300

What word is used to instantiate a subclass?

What about a “sub class” of an interface?

Extends

Implements

300

How would you create a random number between 1 and 50(inclusive)?

int rand = (int)(Math.random()*50)+1;

300

How many bits are there in a megabyte?

8 Million

400

Write code to return the number of elements that are even in an array.  Assume that random numbers have already been initialized for each element in the array.

int[][] arr = new int[5][7];

int count = 0;

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

for(int j = 0; j<7; j++){

if(arr[i][j]%2==0)

count++;

}

}

return count;

400

Write the code to reverse the String: 

String str = “reverse”;

Then return it.

String reversed = “”;

for(int i=str.length()-1; i>=0; i--){

           reversed+=str.charAt(i);

}

return reversed;

400

A class can implement an infinite number of interfaces, but only extend one class at a time.

True

400

Name all 8 primitive data types in java

Boolean, int, char, float, long, bit, byte, double

400

Solve for the following problem, and give the answer in decimal form:

34dec - 25hex + 101001bin = ?

26

500

int[][] matrix = {{1,1,2,2},{1,2,2,4},{1,2,3,4},{1,4,1,2}}; 

int sum = 0;

int j= matrix[0].length - 1; for (int i = 0; i < 4; i++) { sum += matrix[i][j]; 

}

System.out.print(sum);

What number is printed out?

12

500

public static String wordScramble (String str)

 {

 if (str.length() == 0){

    return "!"; 

}

else{

 return wordScramble(str.substring(1)) + str.substring(0,1); 

}

}

What is returned as a result of wordScramble(“programming”)?

!gnimmargorp

500

public class Person 

{

private String name; 

private int age; 


public Person(String n, int a) 

name = n;

 age = a; } 

}


public class Student extends Person 

{

 private int grade;


public Student(String n, int a, int g) 

{

 /* to be completed */

 }

 }

What code correctly completes the Student constructor?

super(n,a);

grade = g; 

500

All classes in java by default extend which class?

Object

500

Write an expression equivalent to:

! ( (x > 12) && (x <= 7) )

(x <= 12) || (x > 7)