• 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

IrixTech Mock Exam

 
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mock Questions from Practice test 1 @ http://www.irixtech.com

Hi sorry this post is so long...I'm just really dubious of this mock exam..

What is the output of the following code when compiled and run ?


Answer:123123

How can this be when the join method has no parameter??

Also:

Given the following,



which is true about the classes depicted above ?

I said that class B is tightly encapsulated.The answer said neither.Isn't B's instance variable marked as private??

Another one:
What will happen if run the following code?



I said the output will be false and false.They said false null.Why null!?!?
They were both declared and accessed in exacty the same way...

Another one:


Here they say that there is a compiler error because x is not initialised.I'm not too sure about this one...

Another:
A source code file contains the following lines:


Doesn't there need to be a public class matching the name of the java file?

And another:



They say compiler error on line 08...Cannot access protected member f.
But doesn't protected allow a subclass to access the protected member as long as you dont modify it??
[ December 23, 2008: Message edited by: Duran Harris ]
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duran you should Use One Thread Per Question

Anyways let me try to solve your queries

the join method has no parameter??



What's the problem with join having no parameters. The code starts a thread and then joins it

19. t1.start( );
20. t1.join( );
21. ...

So the execution will reach to statement 21 when the thread t1 has completed execution i.e. its run method completes.

I said that class B is tightly encapsulated.



But B is-a A. Since A is not encapsulated, so neither is B. The field x breaks the encapsulation.

Boolean[ ] b1 = new Boolean[10];
boolean[ ] b2 = new boolean[10];

They were both declared and accessed in exacty the same way...



Note that one of the array is a Boolean i.e. array of references. Elements in an array of booleans are automatically initialized to false. And elements in an array of references are always initianlized to null.

Here they say that there is a compiler error because x is not initialised.I'm not too sure about this one...



If your class has a final field, it must be initialized before or in the last statement of every constructor (if a constructor calls another constructor then that constructor doesn't need and in fact cannot initialize it).

Doesn't there need to be a public class matching the name of the java file?



Not necessarily. The rule says that if you have a public class, it must be stored in a .java file with the same name as the public class. So you cannot have two public top level classes in the same source file.

But doesn't protected allow a subclass to access the protected member??



You can access a protected inherited member outside the package only through inheritance. You cannot use an instance of the super class to access it...
 
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Reg 1st question: How can this be when the join method has no parameter??



First of all:
t1 and t2 are created two different objects of R. So they wont block each other.

And there is a call t1.join() just after starting t1. That means main thread waits till the time t1 finishes its task. So run() method will be executed once and prints 123. After run() method is executed, main thread resumes and starts t2. As t2 is created with a different object of R, run() method will print 123. Hence the output is: 123123.

I am not sure why you are confused with join() with no parameters.
if you call join() with no parameters, the waiting thread will become runnable just after the other thread has finished.
if you specify the maximum time that a thread can wait for another thread to finish its task, the waiting thread will become runnable after the time has elapsed if the other thread has not finished before that.

Hope this is clear!
 
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

How can this be when the join method has no parameter??



Why, join() does should take any parameter.

t1.join() internally calls join(0).

public final void join() throws InterruptedException {
join(0);
}



Here 0 parameter indicates that current thread should wait for infinite time until t1 thread does not complete its execution.
 
Duran Harris
Ranch Hand
Posts: 608
Eclipse IDE Spring Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Aaah...wow I made dumb mistakes.
Okay so calling t.join() joins the currently executing thread to t?I forgot about main..?Doesn't join have a parameter of(Thread t) or is it optional...I really must go over threads tonight

If your class has a final field, it must be initialized before or in the last statement of every constructor (if a constructor calls another constructor then that constructor doesn't need and in fact cannot initialize it).


So what exactly happens when you declare an instance reference variable as final like this=> final int x;
=> does it only become immutable once a value is
=> assigned to it??
[ December 23, 2008: Message edited by: Duran Harris ]
 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it becomes a compile time constant.
Compiler replaces that variable with its value once it is compiled
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you declare a constant in your class, it must be initialized before constructors are over. So this code will not compile as z is not initialized in the no-arg constructor

 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But if you declare a final AND static variable, then it must be initialized either as part of its declaration, or in a static initializer block exclusively.
 
A "dutch baby" is not a baby. But this tiny ad is baby sized:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic