JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

OCPJP 6 Mock Exam -18

January 22, 2014 by Krishna Srinivasan Leave a Comment

1 . What will be the output?

[code lang=”java”]
public class Test1 extends Base{
Test1(long i){}
public static void main(String args[]){}
}
class Base{
Base(int i){}
}
[/code]

Choose the one below:

  1. Base(){}
  2. Base(long l){}
  3. Base(float f){}
  4. It will compile properly

2 . What will be the output?

[code lang=”java”]
public class Test2 extends Base{
Test2(long i){
super(2);
}
Test2(){}
public static void main(String args[]){
new Test2(2);
}
}
class Base{
Base(int i){
System.out.println(i);
}
Base(byte i){
System.out.println(i*2);
}
}
[/code]

Choose the one below:

  1. 2
  2. 4
  3. Compiler Error
  4. It will compile if we add super(2) in Test2()

3 . which of the following keywords can be applied to constructors?

  1. private
  2. public
  3. void
  4. All the above

4 . which of the following are valid constructors?

  1. Test4(){}
  2. void Test4(){}
  3. private Test4(){}
  4. All the above

5 . which of the following statements are true?

  1. constructors can be overriden
  2. constructors can be overloaded
  3. constructors can return a value
  4. constructors can be static

6 . What will be the output?

[code lang=”java”]
public class Test6{
Test6(int i, int j){
System.out.println("Int");
}
Test6(short i, short j){
System.out.println("Short");
}
Test6(byte i, byte j){
System.out.println("Byte");
}
public static void main(String args[]){
Test6 t = new Test6(1,2);
}
}
[/code]

Choose the one below:

  1. Int
  2. Byte
  3. Short
  4. Compiler error

7 . What will be the output?

[code lang=”java”]
public class Test7{
Test7(int i, int j){
System.out.println("Int");
}
Test7(short i, short j){
System.out.println("Short");
}
Test7(byte i, byte j){
System.out.println("Byte");
}
public static void main(String args[]){
byte b1 = 1;
byte b2 = 2;
Test7 t1 = new Test7(b1,b1);
Test7 t2 = new Test7(b1*1,b1*2);
Test7 t3 = new Test7(b1*b1,b1*b2);
}
}
[/code]

Choose the one below:

  1. Byte Int Int
  2. Byte Byte Byte
  3. Byte Int Byte
  4. Int Int Int

8 . What will be the output?

[code lang=”java”]
public class Test8 extends Base{
Test8(){}
public static void main(String args[]){}
}
class Base{
Base() throws Exception{}
}
[/code]

Choose the one below:

  1. Byte Int Int
  2. Byte Byte Byte
  3. Byte Int Byte
  4. Int Int Int

9 . What will be the output?

[code lang=”java”]
public class Test9 extends Base{
Test9()throws Exception{}
Test9(int i) {}
public static void main(String args[])throws Exception{
new Test9();
}
}
class Base{
Base() throws Exception{
System.out.println("Base");
}
}
[/code]

Choose the one below:

  1. Base
  2. Compiler Error
  3. Runtime exception
  4. None of the above

10 . What will be the output?

[code lang=”java”]
public class Test10 extends Base{
Test10(){}
Test10(int i) {}
public static void main(String args[]){
new Test10();
}
}
class Base{
Base() {
System.out.println("Base");
}
Base(int i) throws Exception{
System.out.println("No argument constructor.");
}
}
[/code]

Choose the one below:

  1. Base
  2. Compiler Error
  3. Runtime exception
  4. None of the above

11 . What will be the output?

[code lang=”java”]
class Test11 extends B {
private Test11() { super(); }
public Test11(String msg) { this(); }
public Test11(int i) {}
}
class A {
public A() {}
public A(int i) { this(); }
}
class B extends A {
public boolean B(String msg) { return false; }
}
[/code]

Choose the one below:

  1. The code will fail to compile
  2. The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or Test11
  3. class Test11 has three constructors
  4. At most one of the constructors of each class is called as a result of constructing an object of class Test11

12 . Which of the following statements are true?

  1. The default constructor has a return type of void
  2. The default constructor takes a sungle parameter
  3. The default constructor takes no parameters
  4. The default constructor is not created if the class has anyconstructors of its own

Answers

1 : 1 is correct. Base(){}.

2 : 4 is correct. It will compile if we add super(2) in Test2().

3 : 1 & 2 is correct.
1 . private
2 . public

4 : 1 & 3 is correct.
1 . Test4(){}
3 . private Test4(){}

5 : 2 is correct. Constructors can be overloaded.

6 : 1 is correct. Int.

7 : 3 is correct. Byte Byte Int.

8 : 3 is correct. Byte Byte Int.

9 : 2 is correct. Compiler Error.

10 : 1 is correct. Base.

11 : 2 & 3 is correct.
2 . The constructor in A that takes an int as argument will never be called as a result of constructing an object of class B or Test11
3 . class Test11 has three constructors

12 : 3 & 4 is correct.
3 . The default constructor takes no parameters
4 . The default constructor is not created if the class has any constructors of its own

Filed Under: Certifications Tagged With: OCPJP, OCPJP 6

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved