public static String swapPairs(String s) {
if (s.length() < 2) return s;
return s.substring(1, 2) + s.substring(0, 1) + swapPairs(s.substring(2));
}
What is the output of swapPairs("IVTCRO");?
Answer: “VICTOR”
Question: What’s the output of the code below?
public interface Charger {
void charge();
}
public class Phone implements Charger {
public void charge() {
System.out.print("Charging... ");
}
}
public class Main {
public static void main(String[] args) {
Charger myCharger = new Phone();
myCharger.charge();
}
}
Answer:
Charging...
Jeopardy questions
public class Person{
private String name;
private int age;
public Person(String person_name, String person_age){
name = person_name;
age = person_age;
}
//getter + setter methods
public void displayUserInfo(){
System.out.println("Hello I am a person! My name is " + name + " and I am " + age + " years old.");
}
}
public class student extends Person{
private int studentID;
private String school;
public Person(String student_name, int student_age, int id, String student_school){
super (student_name, student_age);
studentId = id;
school = student_school;
}
//getter and setter methods
public void displayUserInfo(){
super.displayUserInfo();
System.out.println("I am also a student! I go to " + school);
}
}
public static void main (String [] args){
Student one = new Student(“Jane”, 15, 123456, “Stratford”);
one.displayInfo();
}
What is the output of this code?
The output: "Hello I am a person! My name is Jane and I am 15 years old."
"I am also a student! I go to Stratford"
What does this code print?
Int number = 129832;
while(true) {
System.out.print(number % 10);
number /=10;
if(number < 1) break;
}
Answer: 238921
What GPU got stolen from the data science computer (Vansh can't answer or -100000 points)
Answer: 5080
public static int mystery(int n) {
if (n <= 1) return 2;
return n * mystery(n - 2);
}
Whats the output of mystery(5);?
Answer: 5*3*2 = 30;
Question: What's the output of the code below?
public interface Printable {
int MAX_PAGES = 100;
void print();
}
public class Document implements Printable {
public void print() {
MAX_PAGES = 200;
System.out.println("Printing up to " + MAX_PAGES + " pages.");
}
}
Answer:
Compile error
Explanation:
Line 8 (MAX_PAGES = 200;) attempts to reassign a new value to MAX_PAGES.
All variables declared in a Java interface are implicitly public static final (constants), meaning their values cannot be modified once declared.
super();
Given block variable i for a for loop(i starts at 0), and you want it to run 5 times total with i incrementing by 2, what is the code in java for the header of this for loop?
Answer: For(int i = 0; i<10; i+=2)
public class Test {
public static void main(String[] args) {
String a = new String("hello");
String b = "hello";
String c = a;
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(c == a);
System.out.println(c.equals("hello"));
}
}
What will the code output?
ANSWER: false true true true
public static boolean hasEven(int[] a, int i) {
if (i >= a.length) return false;
return hasEven(arr, i + 1) || (a[i] % 2 == 0);
}
Although this return statement is correct, it has a major efficiency flaw. Identify the flaw and its fix.
Answer:
Placing hasEven(a, i+1) on the left side of “||” forces Java to recurse all the way to the end of the array (O(N) depth) before ever checking if a[i] is even.
The easiest way to fix this is by swapping the even check with the recursive function call
Question: What’s the output of the code below and why?
interface Playable {
void play();
}
class Instrument implements Playable {
public void play() { System.out.print("Instrument "); }
}
class Guitar extends Instrument {
public void play() { System.out.print("Guitar "); }
public void tune() { System.out.print("Tune "); }
}
____________
Line 1: Playable p = new Guitar();
Line 2: p.play();
Line 3: ((Instrument) p).play();
Line 4: p.tune();
Answer:
Line 4 causes a compile-time error.
Explanation:
Lines 2 & 3: Both compile and print Guitar Guitar due to method binding (the actual object in memory is a Guitar). Casting to Instrument in Line 3 does not change the runtime object.
Line 4: Fails to compile because the declared reference type (Playable) does not contain a tune() method. The compiler only checks the reference type, not the actual object at runtime.
Given that there is a parent class Vehicle and a child class Car, and the Car method contains a method accelerate() that prints "accelerate", and the vehicle method contains a method move() that prints "move' that is not present in the child class, Car, what will the following code print, and why:
Code #1:
Vehicle toyota = new Car();
toyota.move();
Code #2:
Vehicle toyota = new Car();
toyota.accelerate();
The first code will print "move"; the second code will produce a compile time error since you cannot access specific methods of the child class if it is not present in the parent class while using Polymorphism.
What does following code print?
int count = 0;
int k = 3;
while (k > 0) {
for (int i = 0; i < k; i++) {
for (int j = i; j < k; j++) {
count++;
}
}
k--;
}
System.out.println(count);
Answer: 10
What day is the APCSA exam on 2027?
Answer: 5/12/27