SCJP Mock exam for Overriding & Overloading


These questions cover Overriding and Overloading.

  1. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        public java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  2. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        protected java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  3. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    public class Parent {
        public java.util.Set<String> set;
    }
    
    package dir2;
    public class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  4. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    public class Parent {
        protected java.util.Set<String> set;
    }
    
    package dir2;
    public class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  5. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  6. These classes are defined in separate files. Will this code compile successfully? (1 correct answer)
    package dir1;
    class Parent {
        private java.util.Set<String> set;
    }
    
    package dir2;
    class Child extends dir1.Parent {
        void test() {
            set.add("Hello");
        }
    }
    
    1. Yes.
    2. No.

  7. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(new Parent().message);
        }
    }
    
    1. parent
    2. child

  8. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            System.out.println(new Child().message);
        }
    }
    
    1. parent
    2. child

  9. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            System.out.println(yo.message);
        }
    }
    
    1. parent
    2. child

  10. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            new Parent().say();
        }
    }
    
    1. parent
    2. child

  11. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            new Child().say();
        }
    }
    
    1. parent
    2. child

  12. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            yo.say();
        }
    }
    
    1. parent
    2. child

  13. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
        void say() {
            System.out.println(message);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            new Parent().say();
        }
    }
    
    1. parent
    2. child

  14. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
        void say() {
            System.out.println(message);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            new Child().say();
        }
    }
    
    1. parent
    2. child

  15. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        String message = "parent";
        void say() {
            System.out.println(message);
        }
    }
    
    class Child extends Parent {
        String message = "child";
        void say() {
            System.out.println(message);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            yo.say();
        }
    }
    
    1. parent
    2. child

  16. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        static void say() {
        }
    }
    
    class Child extends Parent {
        void say() {
        }
    }
    
    1. Yes.
    2. No.

  17. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() {
        }
    }
    
    class Child extends Parent {
        static void say() {
        }
    }
    
    1. Yes.
    2. No.

  18. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() {
        }
    }
    
    1. Yes.
    2. No.

  19. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() throws Exception {
        }
    }
    
    1. Yes.
    2. No.

  20. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    import java.io.*;
    
    class Parent {
        void say() throws Exception {
        }
    }
    
    
    class Child extends Parent {
        void say() throws IOException, FileNotFoundException {
        }
    }
    
    1. Yes.
    2. No.

  21. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() throws RuntimeException {
        }
    }
    
    1. Yes.
    2. No.

  22. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws PovertyException {
        }
    }
    
    class Child extends Parent {
        void say() throws NoFoodException {
        }
    }
    
    class PovertyException extends Exception {
    }
    
    class NoFoodException extends PovertyException {
    }
    
    1. Yes.
    2. No.

  23. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws PovertyException {
        }
    }
    
    class Child extends Parent {
        void say() throws NoFoodException, IOException {
        }
    }
    
    class PovertyException extends Exception {
    }
    
    class NoFoodException extends PovertyException {
    }
    
    1. Yes.
    2. No.

  24. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws NoFoodException {
        }
    }
    
    class Child extends Parent {
        void say() throws PovertyException {
        }
    }
    
    class PovertyException extends Exception {
    }
    
    class NoFoodException extends PovertyException {
    }
    
    1. Yes.
    2. No.

  25. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() throws Exception {
        }
    }
    
    class Child extends Parent {
        void say() throws ArrayIndexOutOfBoundsException,
        ClassCastException, NullPointerException {
        }
    }
    
    1. Yes.
    2. No.

  26. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() {
        }
    }
    
    class Child extends Parent {
        void say() throws ArrayIndexOutOfBoundsException,
        ClassCastException, NullPointerException {
        }
    }
    
    1. Yes.
    2. No.

  27. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        <T> void say() {
        }
    }
    
    class Child extends Parent {
        void say() {
        }
    }
    
    1. Yes.
    2. No.

  28. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say() {
        }
    }
    
    class Child extends Parent {
        <T> void say() {
        }
    }
    
    1. Yes.
    2. No.

  29. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        void say(Number number) {
        }
    }
    
    class Child extends Parent {
        <T extends Number> void say(T number) {
        }
    }
    
    1. Yes.
    2. No.

  30. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        Number get() {
            return 1;
        }
    }
    
    class Child extends Parent {
        Integer get() {
            return 2;
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            System.out.println(yo.get());
        }
    }
    
    1. 1
    2. 2
    3. Compilation fails.

  31. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        Integer get() {
            return 1;
        }
    }
    
    class Child extends Parent {
        Number get() {
            return 2;
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            System.out.println(yo.get());
        }
    }
    
    1. 1
    2. 2
    3. Compilation fails.

  32. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Float get() {
            return 1.0f;
        }
    }
    
    class Child extends Parent {
        Integer get() {
            return 2;
        }
    }
    
    1. Yes.
    2. No.

  33. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Double get() {
            return 1.0;
        }
    }
    
    class Child extends Parent {
        Integer get() {
            return 2;
        }
    }
    
    1. Yes.
    2. No.

  34. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Integer get() {
            return 1;
        }
    }
    
    class Child extends Parent {
        Double get() {
            return 2.0;
        }
    }
    
    1. Yes.
    2. No.

  35. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        void show(Parent parent) {
            System.out.println("parent");
        }
    }
    
    class Child extends Parent {
        void show(Child child) {
            System.out.println("child");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            child.show(child);
    	}
    }
    
    1. parent
    2. child
    3. Compilation fails.

  36. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        void show(Parent parent) {
            System.out.println("parent");
        }
    }
    
    class Child extends Parent {
        void show(Child child) {
            System.out.println("child");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            child.show(parent);
    	}
    }
    
    1. parent
    2. child
    3. Compilation fails.

  37. These classes are defined in the same file. What is the output? (1 correct answer)
    class Parent {
        void show(Parent parent) {
            System.out.println("parent");
        }
    }
    
    class Child extends Parent {
        void show(Child child) {
            System.out.println("child");
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Child();
            Child child = new Child();
            child.show(parent);
    	}
    }
    
    1. parent
    2. child
    3. Compilation fails.

  38. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        static Integer number;
    }
    
    class Child extends Parent {
        Integer number;
    }
    
    1. Yes.
    2. No.

  39. These classes are defined in the same file. Will this code compile successfully? (1 correct answer)
    class Parent {
        Integer number;
    }
    
    class Child extends Parent {
        static Integer number;
    }
    
    1. Yes.
    2. No.

  40. These classes are defined in the same file and compilation succeeds. What is the output? (1 correct answer)
    class Parent {
        Integer a = 1;
        static Integer b = 2;
    }
    
    class Child extends Parent {
        static Integer a = 41;
        Integer b = 42;
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            Parent yo = new Child();
            System.out.format("%d %d %d %d %d %d ",
            parent.a,
            parent.b,
            child.a,
            child.b,
            yo.a,
            yo.b);
        }
    }
    
    1. 1  2  41  42  1  2
    2. 1  2  41  42  41  42
    3. 1  2  41  42  1  42
    4. 1  2  41  42  41  2

  41. These classes are defined in the same file and compilation succeeds. What is the output? (1 correct answer)
    class Parent {
        final Integer a = 1;
        final static Integer b = 2;
    }
    
    class Child extends Parent {
        final static Integer a = 41;
        final Integer b = 42;
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent parent = new Parent();
            Child child = new Child();
            Parent yo = new Child();
            System.out.format("%d %d %d %d %d %d ",
            parent.a,
            parent.b,
            child.a,
            child.b,
            yo.a,
            yo.b);
        }
    }
    
    1. 1  2  41  42  1  2
    2. 1  2  41  42  41  42
    3. 1  2  41  42  1  42
    4. 1  2  41  42  41  2

  42. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Grandparent yo = new Child();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  43. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Child();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  44. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Parent yo = new Parent();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  45. These classes are defined in the same file. What is the output? (1 correct answer)
    class Grandparent {
        String name = "granparent";
        void act() {
            System.out.println(name);
        }
    }
    
    class Parent extends Grandparent {
        String name = "parent";
    }
    
    class Child extends Parent {
        String name = "child";
        void act() {
            System.out.println(name);
        }
    }
    
    public class Test {
        public static void main(String[] args) {
            Grandparent yo = new Parent();
            yo.act();
        }
    }
    
    1. grandparent
    2. parent
    3. child
    4. Compilation fails

  46. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method() {
        }
        static void method() {
        }
    }
    
    1. Yes.
    2. No.

  47. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method() {
        }
        static void method() throws Exception {
        }
    }
    
    1. Yes.
    2. No.

  48. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method() {
        }
        static Object method() {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  49. Will this code compile successfully? (1 correct answer)
    import java.util.List;
    class Overload {
        public void method(List<String> names) {
        }
        private static Object method(String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  50. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String[] names) {
        }
        final Object method(String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  51. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String[] names) {
        }
        final Object method(String name, String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  52. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String name, String[] names) {
        }
        final Object method(String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  53. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String name, String[] names) {
        }
        final Object method(String name, String... names) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  54. Will this code compile successfully? (1 correct answer)
    class Overload {
        public void method(String name, String[] names) {
        }
        final Object method(String... names, String name) {
            return null;
        }
    }
    
    1. Yes.
    2. No.

  55. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    class Overload {
        void method(NavigableSet<Integer> set) {
        }
        void method(NavigableSet<String> set) {
        }
    }
    
    1. Yes.
    2. No.

  56. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    class Overload {
        void method(SortedSet<String> set) {
        }
        void method(NavigableSet<String> set) {
        }
    }
    
    1. Yes.
    2. No.

  57. Will this code compile successfully? (1 correct answer)
    import java.util.*;
    class Overload {
        void method(Set<String>... set) {
        }
        void method(Set<String> set) {
        }
        void method(NavigableSet<String> set) {
        }
    }
    
    1. Yes.
    2. No.

  58. Will this code compile successfully? (1 correct answer)
    import java.util.NavigableSet;
    
    interface Interface {
        void method(NavigableSet<String> set) throws Exception;
    }
    
    public class Overload implements Interface {
        void method() {
        }
        void method(NavigableSet<String> set) throws Exception {
        }
    }
    
    1. Yes.
    2. No.

  59. These classes are defined in the same file. What is the output of this code? (1 correct answer)
    class Parent {
        int value;
        void validate() {
            value = value + 10;
        }
    }
    
    class Child extends Parent {
        void validate() {
            super.validate();
            value = value - 2;
        }
    }
    
    public class Run {
        public static void main(String[] args) {
            Child child = new Child();
            child.validate();
            System.out.println(child.value);
        }
    }
    
    1. It prints “8”.
    2. It prints “-2”.
    3. None of the above.

  60. These classes are defined in the same file. What is the output of this code? (1 correct answer)
    class Parent {
        int value;
        void validate() {
            value = value + 10;
        }
    }
    
    class Child extends Parent {
        int value;
        void validate() {
            super.validate();
            value = value - 2;
        }
    }
    
    public class Run {
        public static void main(String[] args) {
            Child child = new Child();
            child.validate();
            System.out.println(child.value);
        }
    }
    
    1. It prints “8”.
    2. It prints “-2”.
    3. None of the above.

© 2008 Nikos Pougounias. This is a free contribution to the Java community. Please distribute it for free. https://nikojava.wordpress.com

Answers

  1. b
  2. b
  3. a
  4. a
  5. b
  6. b
  7. a
  8. b
  9. a
  10. a
  11. a
  12. a
  13. a
  14. b
  15. b
  16. b
  17. b
  18. a
  19. a
  20. a
  21. a
  22. a
  23. b
  24. b
  25. a
  26. a
  27. a
  28. b
  29. b
  30. b
  31. c
  32. b
  33. b
  34. b
  35. b
  36. a
  37. a
  38. a
  39. a
  40. a
  41. a
  42. c
  43. c
  44. a
  45. a
  46. b
  47. b
  48. b
  49. a
  50. b
  51. a
  52. a
  53. b
  54. b
  55. b
  56. a
  57. a
  58. b
  59. a
  60. b

An index of all the SCJP Mock exams may be found here.

21 Responses to SCJP Mock exam for Overriding & Overloading

  1. Srilatha says:

    All questions are really helpful in understanding the concept of overloading/overriding. Thanks for posting the same!
    I have one doubt regarding the question 37. As per me, the answer should be a. I have run and saw the result as a. So could you please tell me why the answer is given as b?

  2. Nikos says:

    Indeed the answer of 37 should be a. The issue has been resolved. Thank you!

  3. Aruna says:

    Very Good Practice Material.Thanks.

  4. […] Free SCJP Mock exam for Overriding & Overloading […]

  5. Pranjal says:

    thanks for the good & free practice material:-)

  6. principecr says:

    Wordefull cases . Thank you

  7. Anand says:

    Very helpful in final stages of preparation.Questions cover numerous possibilities. Thanks.

  8. Dark_Ichigo says:

    54 is (a)…………..Iv copied the code and tested it 5 times!

    I don’t think there’s anything wrong with it??

  9. Jyo_Java says:

    Really very helpful when preparing for certification but can somebody tell me what is wrong with question number 58. I am able to compile this very well.As per my knowledge the answer shuld be “a”.

  10. wilson says:

    The reason the answer is b for question 58 is that in Java you cannot reduce the accessibility of a method when it is overriden. method declared in an interface is implicitly public but when it is implemented by a concrete class a default is given hence reducing the accessibility, the compiler would complain about this. in short, changing from public to default is not allowed in Java.

  11. Amir says:

    @Dark_Ichigo
    only works like that… in actual… overloaded have varg Parameter at forst place then string..thats wrong

    public void method(String name, String… names) {
    }
    final Object method(String[] names, String name) {
    return null;
    }

  12. Yogesh says:

    Hi, the content is very useful..
    Could somebody explain question number 37?

  13. Amar says:

    Parent parent = new Child();
    Child child = new Child();
    child.show(parent); –>in that where reference goes to the parent class .because in that where i call the show method with Child class object but show is taken the object of parent class that’s reference goes to the parent.

  14. Eric says:

    Amazingly detailed!..Thanks!

  15. dml says:

    I think Question 27 is wrong,
    void say(){} : malformed signature of method, if method returns something, can’t be ‘void’ and must have return statement in the body

  16. niks551 says:

    I think answer to question 36 is wrong .It should be compilation fails.

    Reason:
    We cannot pass a super class reference where we are expecting a subclass reference.

  17. niks551 says:

    Aggh please ignore the previous comment.

  18. I’ve not understood question 11. Why “parent” and not “child”? Now I haven’t a reference to the Parent. It’s only an inherited method invoked by the Child instance on his own instance variable. Where is my error? I know that Polimorphism at runtime is valid only for instance methods, but here we haven’t Parent references…

  19. Paulo Mendes says:

    Very good material. The best I’ve found until now! Thank you very much.

  20. surya says:

    Good stuff man!!! Very useful one.Keep going..
    Thank u very much…

  21. It covers all cases of overriding and overloading….good article..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: