Skip to content

Instantly share code, notes, and snippets.

@raykolbe
Last active September 4, 2016 21:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save raykolbe/579bba00941ec16b3bce975679abbc8b to your computer and use it in GitHub Desktop.
Save raykolbe/579bba00941ec16b3bce975679abbc8b to your computer and use it in GitHub Desktop.
OCAJP8 Question Bank

##Description While studying for the OCAJP8 exam I have created hundreds (if not more) of small code examples that help test my knowledge. The purpose of this document is to help myself and others test their knowledge of Java 8 in preparation for the OCAJP8 exam. Use this document as-is or as a starting point to create your own question bank. Feel free to contribute by commenting on this gist.

At some point it might make sense to turn this gist into a proper repo for better organization of code and contributions.

This is a living document

##License MIT

##Questions

  1. What is the output?
int x = 0;
x = x++;
System.out.println(x);

  1. What is the output?
int i = 5;
int j = 10;
System.out.println(++i + --i * --j);

  1. What is the output?
int x = 1;
int y = 1;
x += y += x += y;
System.out.println("x=" + x + ", y=" + y);

  1. What is the output?
int a = 2;
a += ++a + a * a;
System.out.println(a);

  1. How could you get this to compile and run?
Integer a = 2;
Float b = 3f;
Double c = a * b;
System.out.println(c);

  1. What is the output?
public class Scratch {
    Integer Scratch() {return i;}
    private Integer i;
    public static void main(String args[]) {
        Scratch s = new Scratch();
        int e = s.i;
        System.out.println(e);
    }
}

  1. What is the output?
interface Calculator {}
class FunCalculator implements Calculator {
    int x;
    int y = ++x + calc(x); {x=+x;}
     
    private int calc(int x) {
        return x * 2;
    }
}
 
public class Scratch {
    public static void main(String args[]) {
        FunCalculator a = new FunCalculator();
        System.out.println(a.x);
        System.out.println(a.y);
    }
}

  1. What is the output?
interface Calculator {}
class FunCalculator implements Calculator {
    public int x;
    int y = ++x + calc(x); {x=+x;}
     
    int calc(int x) {
        return x * 2;
    }
 
    int calc(int... x) {
        return 52;
    }
}
 
public class Scratch {
    public static void main(String args[]) {
        Calculator a = new FunCalculator();
        System.out.println(a.x);
        System.out.println(a.y);
    }
}

What is the output?

int[] x = {0, 1, 2, 3, 4};
int[] y = {4, 3, 2, 1, 0};
int z = 0;
x[++z] = z = y[z] = 3 - 1 * 2 + z;
System.out.println("z="+z);
System.out.println("x="+java.util.Arrays.toString(x));
System.out.println("y="+java.util.Arrays.toString(y));

  1. What is the output?
public class Scratch {
    void add(Integer a) {
        a++;
    }

    void add(int a) {
        a++;
    }

    public static void main(String args[]) {
        int a = 0;
        Integer b = 0;

        Scratch s = new Scratch();
        s.add(a);
        s.add(b);
        System.out.println("a=" + a);
        System.out.println("b=" + b);
    }
}

  1. What is the output?
class Cat {
    void Cat(int lives, int... counts) {
        System.out.println("int lives and vararg counts!");
    }

    public void Cat(Integer lives, int[] counts) {
        System.out.println("Integer lives and array counts!");
    }
}

public class Scratch {
    public static void main(String args[]) {
        Integer x = 9;
        int[] y = {1,2};

        Cat c = new Cat();
        c.Cat(x, y);
    }
}

  1. What is the output?
abstract class Mammal implements Animal {
    public static void jump() {
        System.out.println("Mammal jumped!");
    }

    public void jumpHigh() {
        jump();
    }
}

class Human extends Mammal {
    public static void jump() {
        System.out.println("Human jumped!");
    }

    public void jumpHigh() {
        jump();
    }
}

public class Scratch {
    public static void main(String args[]) {
        Mammal m = new Human();
        m.jump();
        m.jumpHigh();
    }
}

  1. What is the output?
public class Scratch {
    static void sniff(Long l) { System.out.println("Long"); }
    static void sniff(Number... n) { System.out.println("Number"); }
    static void sniff(Object o) { System.out.println("Object");}

    public static void main(String args[]) {
        sniff(12);
        sniff(12L);
        sniff((Long) 12L);
    }
}

  1. What is the output?
public class Scratch {
    public int count = 12;
	void Scratch() {
		count++;
	}

	public static void main(String args[]) {
		Object o = new Scratch();
		System.out.println(o.count);
    }
}
  1. What is the output?
class Base {
    int times;
    Base(int times) {
        times = this.times;
    }
}
public class Scratch extends Base {
    public Scratch(int times) {
        super(times);
    }

    public static void main(String args[]) {
        Scratch s = new Scratch(120);
        System.out.println(s.times
    }
}

  1. What is the output?
class Base {
    static { System.out.println("A"); }
    static int times;
	
    Base(int time) {
        times = time;
        System.out.println("B");
    }

    { System.out.println("C"); }
    static { System.out.println("D");}
}

public class Scratch extends Base {
    static { System.out.println("E"); }
    public Scratch(int times) {
        super(times);
        System.out.println("F");
    }

    public static void main(String args[]) {
    }
}

  1. Which of the following are incapable of holding 3 strings?
ArrayList<String> strings = new ArrayList<>();
ArrayList<String> strings = new ArrayList<String>();
ArrayList strings = new ArrayList();
List<String> strings = Arrays.asList(new String[]{"Hello", "World"});

  1. What is the output?
interface Account {
    float rate = 0.05;
    default float calculate(float amount) {
        return (rate * amount) + amount;
    }
}

public class Scratch {
    public static void main(String args[]) {
    	float total = Account.calculate(100);
        System.out.println(total);
    }
}

  1. What is the output?
int[] nums = {12, 6, 2, 4};
nums = java.util.Arrays.sort(nums);
System.out.println(java.util.Arrays(nums));

  1. How could you get this to compile and run? What is the output?
public class Scratch {
    public static void main(String args[]) {
        try {
            printMessage();
        } catch (WillItException exception) {
            System.out.println("Exception!");
        }
    }

    private static void printMessage() {
        System.out.println("It prints!");
    }
}

class WillItException extends Exception {}

  1. What is the output?
public class Scratch {
    public static void main(String args[]) {
        try {
            printMessage();
        } catch (Exception e) {
            throw new RuntimeException("Caught exception!");
        } finally {
            throw new RuntimeException("Finally exception!");
        }
    }

    private static void printMessage() throws WillItException {
        System.out.println("It prints!");
    }
}

class WillItException extends Exception {}

  1. What is the output?
public class Scratch {
    public static void main(String args[]) {
        try {
            new Sniff().printMessage();
        } catch (WillItException e) {
            System.out.println("Caught!");
        }
    }

    protected void printMessage() {
        System.out.println("Scratch!");
    }
}

class Sniff extends Scratch {
    public void printMessage() throws WillItException {
        System.out.println("Sniff!");
    }
}

class WillItException extends Exception {}

  1. What is the output?
public class Scratch {
    public static void main(String args[]) {
        test((short) 127);
    }

    private static void test(float f) {
        System.out.println("Float");
    }
	
    private void test(int i) {
        System.out.println("Int");
    }
}

  1. What is the output?
public class Scratch {
    public static void main(String args[]) {
        short a = 127;
        Scratch s = new Scratch();
        s.calc(a);
    }

    protected void calc(int i) {
        System.out.println("int called");
    }

    static void calc(Short s) {
        System.out.println("Short called");
    }
}

  1. What is the output?
boolean u = true;
do {
    System.out.println(u);
} while (u ^ u);

  1. Given the following two files what is the output?
// study/stack/Paper.java
package study.stack;

public class Paper {
    protected void print() { System.out.println("Printing..."); }
    public void draw() { System.out.println("Drawing..."); }
}

// study/Scratch.java
package study;

import study.stack.*;

public class Scratch extends Paper {
    public static void main(String args[]) {
        Scratch s = new Scratch();
        s.draw();
        s.print();
	
        Paper p = new Scratch();
        p.draw();
        p.print();
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment