• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Mock exam queries

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 coderanch.

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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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. .?
 
Smitha Ballikar
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic