Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubts from javaprepare sample test

 
Renuka Kilambi
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the below question from the java prepare test , My answer was
a and c.But in the key the answer is given as a,c,d. How come? A class Constructor may have keyword private also.Am I wrong?An explanation is highly appreciated.
Renuka
Q. Which of the following is correct ? Select all correct answers.

a.The native keyword indicates that the method is implemented in another language like C/C++.
b.The only statements that can appear before an import statement in a Java file are comments.
c.The method definitions inside interfaces are public and abstract. They cannot be private or protected.
d.A class constructor may have public or protected keyord before them, nothing else.
 
Aru
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Renuka,
U are absolutely correct. A class can have public / private / protected constructors
A class can be designed to prevent code outside the class declaration from creating instances of the class by declaring at
least one constructor, to prevent the creation of an implicit constructor, and declaring all constructors to be private.
See the follwing code
class Base{
private Base(){
System.out.println("Private Base Constructor");
}
}

public class Test extends Base {
Test(){
System.out.println("Child Constructor");
}
public static void main (String args []) {
new Test();
}
}
This code gives compilation error
Test.java:8: No constructor matching base() found in class Base. Test(){

Because a class having a private constructor can't be instantiated (here Test is a form of base).
Hope this clears your doubt.
Aruna.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pls. anyone can explain for the second question.
 
maha anna
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the question Renuka posted above , the option d) is NOT valid. We can very well have private constructors for a class. So let us move this thread to our 'Mock Exam Errata'.
Before that, Surya, I am not sure of what you ask. Can you rephrase your qstn? Are you talking about another qstn in javaprepare.com. If so please start another NEW THREAD so that it is easy for us to move this thread to Errata Forum. I will close this thread after confirmation from you.Thank you.
regds
maha anna
[This message has been edited by maha anna (edited July 21, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry I misunderstood that this second question is same one of mine which is another thread.
Sorry for any inconvenience.
Thanks for your reply.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic