Q1) 2pts each for a), b), c),f). 3pts each for d) e)
WhileTest
2
1
ForTest
00000
2222
444
StringTest
1: 10
2: i
3: 3
4: -1
StaticTest
1: 6
2: 9
3: 7
4: 11
5: 5
6: ab
Sub/Super
1: 2, 4, 2, 2, 2
2: 10, 11, 0, 1, 0
3: 11, 13, 1, 1, 1
Test
1: 4.4
2: 5.5
3: 4.4
4: 5.5
Q2)
1 pt for each mistake found, 1 pt for correction.
Error1
a)
public class Error1 {
public void method(int a) {
a = 3;
return;
}
public void method() {
System.out.println("Hi");
return;
}
public double method2() {
return 3.5/2;
}
public double method2(int a) {
return a++;
}
}
b)
public class Error2 {
public static void main(String[] args) {
int a=1;
if(a==1) {
System.out.println("a is 1");
} else {
System.out.println("a is not 1");
}
float f = 2.3f;
if(f > 2.1) {
System.out.println("f is greater than 2.1");
}
}
}
public class abstract ClassB extends ClassA {
public int method(int a) {
return 2*a;
}
public int method(int a, int b) {
return a + b;
}
}
Q3)
a) 3,4,5,6
b)1,4
Q4)
public class IntArray {
int[] cut(int[] array, int max) {
int num = 0;
// 4pts
for(int i = 0; i < array.length; i++) {
if(array[i] <= max) {
num++;
}
}
int[] result = new int[num]; // 1pt
int j = 0;
// 5pts
for(int i = 0; i <= result.length; i++) {
if(array[i] <= max) {
result[j] = array[i];
j++;
}
}
return result; //1pt
}
}
Q5
public class CountABC {
public static int count(String s) {
int count = 0;
int start = 0;
String find = "ABC";
while(s.indexOf(find, start) != -1) {
start = s.indexOf(find, start) + 1;
count++;
}
return count;
}
public static void main(String[] args) {
String s = "abcABCdefABC123456ABC###ABC%%%";
System.out.println(count(s));
}
}