wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Mock exam queries Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Mock exam queries" Watch "Mock exam queries" New topic
Author

Mock exam queries

Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Hi friends,

I am giving some mock exams and finding some concepts difficult to understand.I have mentioned a couple of snapshots below.

1.static can be applied to : instance variables, methods,code Segments and classes
Ans:false(i feel true)

2.
float f1 = 1.2F;
float f2 = 1.2F;
if( f1.equals(f2))
System.out.println("We are Equal");
Does it print "We are Equal"

What is the rule of equals for primitives??

3.
Given the following classes declaration in the same file MyClass1.java

package mypackage;

public class MyClass1{
//Some Valid Code
}

protected class MyClass2{
//Some Valid Code
}

A.Two classes can never be declared in the same file.
B.The code Does't compile as the top most class is
protected.
C.The code compiles & MyClass2 can only be instantiated
in it's sub classes.
D.The code compiles & MyClass2 can only be instantiated
by the classes in the package 'mypackage'.

Answer given:B
I feel C.The topmost class is MyClass1 which is public.

4.

Question 40.

Given the following code segments, select the ones that are legal.

A.boolean b = true;
int i = 10;
String s = b + i;

B.char c = 10;
int i = 20;
c += i;

C.String s = "Hello" ;
int i = 10;
i += s;

D.boolean b = null ;
if( b == null ){};

E.String s = "Hello" ;
int i = 10;
s += i;

F.Integer ii = new Integer(10);
int i = 10;
ii += i;

Answer Given :B,E
I feel D,E.If B is correct why not A.



5.

byte b = 20;
char c = b;

Is not not valid casting..
From my understanding,If its a widening conversion and value is in range then no cast is needed..
Answer Given:No
What is the concept here??

6.Is calling super.methodname()in any method should be the first statement or its only for constructors??


Thanks in Advance..
Looking forward for help.

Regards














web page
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
Originally posted by Smitha Amey Ballikar:


1.static can be applied to : instance variables, methods,code Segments and classes
Ans:false(i feel true)

5.

byte b = 20;
char c = b;

Is not not valid casting..
From my understanding,If its a widening conversion and value is in range then no cast is needed..
Answer Given:No
What is the concept here??



1. Instance variables cannot be static by definition. They are associated with an instance of a class, while static members are associated with the class itself.

5. The conversion here is not widening because byte is signed and char is unsigned hence you can represent values with byte that you cannot with char.


SCJP 1.4, SCWCD 1.4
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
For your third question,

Maybe one might has to think this way:

The very name given to these words are Access Modifiers. And each of them have a specific purpose -

(1) Public: <public class Hello> Any class can access this class.
(2) Protected: Only the class that extends this kind of a class has accessibility to this class (even outside the package).
(3) Private: Nothing, even within the package, can access this kind of a class.
(4) Default (when you just define <class Hello> : Only classes within the package can access this class.

So, it does not make sense to name a class with two or more modifiers. Hope this help.

-Vijay
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Originally posted by Vijay Gade:
For your third question,

Maybe one might has to think this way:

The very name given to these words are Access Modifiers. And each of them have a specific purpose -

(1) Public: <public class Hello> Any class can access this class.
(2) Protected: Only the class that extends this kind of a class has accessibility to this class (even outside the package).
(3) Private: Nothing, even within the package, can access this kind of a class.
(4) Default (when you just define <class Hello> : Only classes within the package can access this class.

So, it does not make sense to name a class with two or more modifiers. Hope this help.

-Vijay


The concept here (In Smitha's example) is that the top level class can not be protected. Protected is only allowed for nested or inner classes.

Devender Thareja.


Devender Thareja
SCEA, SCBCD, SCJP
Priya Jothi
Ranch Hand

Joined: Jul 13, 2004
Posts: 168
Hi Smitha,

Hope u got the answers for ur 1st & 3rd questions!!.

Now here r the answers for the rest...

Q2 :

float f1 = 1.2F;
float f2 = 1.2F;
if( f1.equals(f2))
System.out.println("We are Equal");
Does it print "We are Equal"

What is the rule of equals for primitives??


A2 : Equals method is inherited from the Object class which present at the top of the hierarchy.So any object(instance of any class) can inherit this method such as String & any other user defined class.Here u r trying to compare two primitive values using equals method..think from where it'll or it can inherit this method???
So u can't compare two primitive values using equals method.It should be compared only with == operator!!.This is the rule for comparing primitive values.

Q4 :

4.

Question 40.

Given the following code segments, select the ones that are legal.

A. boolean b = true;
int i = 10;
String s = b + i;

B. char c = 10;
int i = 20;
c += i;

C. String s = "Hello" ;
int i = 10;
i += s;

D. boolean b = null ;
if( b == null ){};

E. String s = "Hello" ;
int i = 10;
s += i;

F. Integer ii = new Integer(10);
int i = 10;
ii += i;

Answer Given :B,E
I feel D,E.If B is correct why not A.


A4 : D is not correct.Because boolean can accept only true or false.Null can't be assigned to a boolean.

B is correct.Because addition is carried out between nos and += operator will implicitly do the casting for you.So it'll perfectly compile.

A is not correct.Because there we are trying to add boolean with int & store the result in String.Think what sense it'll make adding boolean with int??!!. So this is not allowed.


Q5 :
5.

byte b = 20;
char c = b;

Is not not valid casting..
From my understanding,If its a widening conversion and value is in range then no cast is needed..
Answer Given:No
What is the concept here??


A5 : What u said is perfectly correct if variable c belongs to short or int data type!!.But here char is unsigned .. where as byte is signed.So compiler would expect us to accept the loss of precision(even though this may not happen) by explicit casting!.the code will compile if we modify the second line as char c = (char)b;

Q6 :

6.Is calling super.methodname()in any method should be the first statement or its only for constructors??


A6 : This rule is applicable to only constructors where as other instance methods has no such restriction!!.

Regards,
Priya.
Sergei Iakhnin
Ranch Hand

Joined: Nov 09, 2004
Posts: 53
Originally posted by Priya Jothi:
So u can't compare two primitive values using equals method.It should be compared only with == operator!!.This is the rule for comparing primitive values.



Just as a sidenote, it is suggested that you not compare floating point values for equality using == but rather use

Float.floatToRawIntBits(float);
and
Double.doubleToRawLongBits(long);

to get the two numbers' bit representations and compare those.
Vijay Gade
Ranch Hand

Joined: Jul 04, 2005
Posts: 81
3.
Given the following classes declaration in the same file MyClass1.java

package mypackage;

public class MyClass1{
//Some Valid Code
}

protected class MyClass2{
//Some Valid Code
}

A. Two classes can never be declared in the same file.
B. The code Does't compile as the top most class is
protected.
C. The code compiles & MyClass2 can only be instantiated
in it's sub classes.
D. The code compiles & MyClass2 can only be instantiated
by the classes in the package 'mypackage'.

Answer given:B


The reply to that, for which Devender had again added an answer is as follows:

quote:
--------------------------------------------------------------------------------
Originally posted by Vijay Gade:
For your third question,

Maybe one might has to think this way:

The very name given to these words are Access Modifiers. And each of them have a specific purpose -

(1) Public: <public class Hello> Any class can access this class.
(2) Protected: Only the class that extends this kind of a class has accessibility to this class (even outside the package).
(3) Private: Nothing, even within the package, can access this kind of a class.
(4) Default (when you just define <class Hello> : Only classes within the package can access this class.

So, it does not make sense to name a class with two or more modifiers. Hope this help.

-Vijay
--------------------------------------------------------------------------------



The concept here (In Smitha's example) is that the top level class can not be protected. Protected is only allowed for nested or inner classes.

Devender Thareja.



Well, I am a little confused here, as I myself am appearing for the exam, and still a learner. How is it that the top level class mentioned here is MyClass1? It definitely is not MyClass2 right? So it has to be MyClass1. Which brings us to the concept of modifiers within the same class file, as opposed to the concept of inner or nested classes.
Please clear my doubt.

Thanks,

-Vijay
Arul Prasad
Ranch Hand

Joined: Jan 20, 2005
Posts: 57
Hi vijay
Well, I am a little confused here, as I myself am appearing for the exam, and still a learner. How is it that the top level class mentioned here is MyClass1? It definitely is not MyClass2 right? So it has to be MyClass1. Which brings us to the concept of modifiers within the same class file, as opposed to the concept of inner or nested classes.
Please clear my doubt.


Inner class is the one which was declared inside a Class
for e.g
class MyClass
{
class MyInner
{

}
}

in the above code Myclass consider to be an outer class
MyInner is considered as a InnerClass or nested class

but inside one .java file u can have more than one classes it was not mean to inner classes

Note: u r outer class cannot be a private nor protected ,but incase of the inner classes we can make it as private or protected Got clear. .?


With Regards<br />Arul
Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Hey friends..

Thanks for all your replies..
It feels great to understand the boring concept by having discussions like this!!!

Cheers...
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Originally posted by Arul Prasad:
Hi vijay


Inner class is the one which was declared inside a Class
for e.g
class MyClass
{
class MyInner
{

}
}

in the above code Myclass consider to be an outer class
MyInner is considered as a InnerClass or nested class

but inside one .java file u can have more than one classes it was not mean to inner classes

Note: u r outer class cannot be a private nor protected ,but incase of the inner classes we can make it as private or protected Got clear. .?


To add more on Arul's comments:
Following is called nested class:
class MyClass
{
static class MyInner
{

}
}

The nested class is not as intimate with outer class as inner class. But that's a different discussion altogather.

Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mock exam queries
 
Similar Threads
valiveru exam errata - please confirm REAL answers, thanks!
Quw from KB book, chap 4 -> test Q 3
About Wrapper classes.
Clrify results from SCJP mock exam site
Top level class??