• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Need Clarification on Mock SCJP Exam Questions

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,

I had been trying some mock SCJP exam questions kindly provided by John Meyers (http://www.badongo.com/file/1591104), and I need some clarification on some of the solutions. I will greatly appreciate it if someone can answer the questions I have below.

Question 4:
int[] a = null , b [] = null; //line 1
b = a;
System.out.println( b );

Solution states that b is a two dimension array. I am not used to the syntax on line 1. Is it equivalent to

int[] a = null;
int[] b [] = null; ?

Question 6:
class test
{
public static void main(String[] args)
{
test inst_test = new test();
String pig[][] = { {"one little piggy"}, {"two little piggies"}, {"three little piggies"} };
for ( Object []oink : pig )
{
for ( Object piggy : oink )
{
System.out.println(piggy);
}
}
}
}

Solution states that "one little piggy two little piggies three little piggies" will be printed. However, the for loop has type of Object. Doesn't it need to be downcasted to string or overwrite the "toString" of test so that it can printed properly?

Question 41
package packed;
public class pack
{
static public int x1 = 7;
static protected int x2 = 8;
static int x3=9;
static private int x4 =10;
}

import packed.pack;
class test extends pack
{
public static void main( String args[] )
{
pack p = new pack();
System.out.println( pack.x2 );
}
}

Solution states that 8 will be printed. Since the variable is static it can still be accessed from another package. If it were not static you would get an error saying protected member access error. I don't understand the solution too well. How does the fact that the variable is static have to do with it being allowed to be accessed from another package? furthermore, if the variable is not static, I thought it can still be accessed. "Protected" modifier allows members to be accessed by subclass even if it is in a different package. Since test is a subclass of pack, why can it not access a protected member of pack?

Question 69
class test
{
test tester;

test( test t )
{
tester = t;
}
test()
{}
public static void main( String args[] )
{
test t = null;
t = new test(); // line 1
t = new test(t); // line 2
t = null; // line 3
}
}

Solution states that 2 objects will be eligible for garbage collection. I thought 4 objects will be eligible. After line 1, a test object is created with a variable tester (2 objects). After line 2, another test object is created with its own variable tester (another 2 objects). When reference is finally nulled on line 3, isn't there a total of 4 objects for garbage collection?
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ans to Q6: Here we are simply assigning a String object to Object Class reference. At the run time, overridden toString() method(overridden by String) gets called because overriding has something to do with the object not reference.

Ans to Q 41: If we don't use static keyword, we can't access protected member through superclass reference, surely we can access protected member in subclass but through subclass reference only.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] a = null , b [] = null; //line 1
b = a;
System.out.println( b );

line 1 places [] immediately after the datatype declaration, so all variables declared on the same line after the "," will be an int[]. So in above case b=a will not compile but the following will
int c[][] = b;

If you put the square brackets after the variable name like int a[] then line 2 will compile.

As an example consider the following, will it compile? If yes then what will be the datatype of d.

int a[] = null , b [] = null, d;
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi David,

Thanks for trying out the mock exam. I guess the others have done justice in answering the questions you asked. They got here before me

As for the garbage collection question, when you have an object 'new test()' you can have a million references pointing to this object. But there is still only one object. When you null out a reference that points to the object it becomes eligible for GC. In this case

t -> test() and the tester of test() points to another object making them both eligible for GC when t is nulled out.
 
David Yu
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks again for the quick responses and knowledge sharing! =D
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey ahmed , I would like to ask

int[] b[];

is a valid declaration. meaning int[] points to an int arr variable.which makes it 2d array.

but i am not getting why after declaring a as
int[] a[], the code

b=a becomes valid ? Please any explaination you can give. The code runs , i have tried out.

Also why assigning null to int arays is permissible?
int types cant be initialized to null..they should be initialized to 0 or some integer value.

Also the code you have shown abt var d , if you assign

d= null; //invalid
d = 0; // perfectly valid.

similarly compiler should give error at
int[] a = null.
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic