How would you initialize an int array with 10 elements?
int[] arr = new int[10];
String str = ”string”;
Create an int with the length of string str.
int len = str.length();
Answer the statement as true or false:
An abstract object may be directly instantiated.
False
The private access modifier in java does what?
Private means it is only accessible in the class it is initialized in
What sort would be most efficient for sorting an array of 1 million elements?
Merge Sort
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”);
String a = “java”;
String b = “language”;
String c = a+b;
System.out.println(c);
What is printed out?
javalanguage
Answer the statement as true or false:
An interface object may be directly instantiated.
False
The public access modifier in java does what?
Public means it is accessible from anywhere
public int mystery(int m)
{
if (m == 1)
{
return 3;
}
else
{
return 3 * mystery(m - 1);
}
}
What does mystery(5) return?
243
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
String a = “apple”;
String b = “pie”;
String c = a.substring(1, 4) + b.substring(1);
System.out.println(c);
What is printed out?
ppleie
What word is used to instantiate a subclass?
What about a “sub class” of an interface?
Extends
Implements
How would you create a random number between 1 and 50(inclusive)?
int rand = (int)(Math.random()*50)+1;
How many bits are there in a megabyte?
8 Million
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;
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;
A class can implement an infinite number of interfaces, but only extend one class at a time.
True
Name all 8 primitive data types in java
Boolean, int, char, float, long, bit, byte, double
Solve for the following problem, and give the answer in decimal form:
34dec - 25hex + 101001bin = ?
26
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
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
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;
All classes in java by default extend which class?
Object
Write an expression equivalent to:
! ( (x > 12) && (x <= 7) )
(x <= 12) || (x > 7)