[HD Pg 374, Sec. 2.8.0 - exercises]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

[HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

I was fooling around with 1st exercise and created the following packages,

package ocajp.ch2.ex1.class1;

import ocajp.ch2.ex1.class2.Class2;
import static ocajp.ch2.ex1.class2.Class2.second_num;

public class Class1 {

public String first_class = "Hi from class1";
public static int first_num = 12;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

System.out.println(second_num);
System.out.println(new Class2().second_class);

}

}

package ocajp.ch2.ex1.class2;

import ocajp.ch2.ex1.class1.Class1;
import static ocajp.ch2.ex1.class1.Class1.first_num;

public class Class2 {

public String second_class = "Hi from class2";
public static int second_num = 22;

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

System.out.println(first_num);
System.out.println(new Class1().first_class);

}

}

They both compile and run and print each other's values.

My question,

1. Is it a good practice to create such, lets call it, cyclical dependency between two packages?
2. If not, what is a good design work-around to answer the second part of the 1st question?

By the way, about question 4; is there any situation that args.length does not represent the number of arguments?

Thanks

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

1. No, it is not a good practice to have cyclic dependencies between two classes.
2. It really depends on the what the classes are trying to achieve (the business logic). Generally, you can reduce one degree of coupling by separating out a part of the business logic into a separate (third) class. For example, if A and B have cyclic dependency, you can introduce a third class C containing partial functionality of A such that A depends on B and B depends on C.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

Thanks for the clarification.

As for question 4, I take it that args.length always is synonymous with number of arguments?

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

Yes, assuming that args is the formal parameter name in the main method, args.length will always refer to the number of arguments unless you make args point to a different array, of course.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

"Number class defines byteValue and shortValue methods and declares intValue, longValue, floatValue, and double- Value methods."

Why doesn't it define all of them?

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

Yes, odd. shortValue could have been abstract as well.
But that's a question for the language/API designers :)
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

It seems that Java designers have tried to keep a very tight leash on anything that has anything to do with -128 to 127 range!

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

1. Define a reference type named Bird. Definean instance method named fly in Bird. Define
a few instance as well as static variables of type int, float, double, boolean, and String in Bird.
2. Create a TestClass that has a static variable of type Bird. Initialize this variable with a valid Bird object.
3. Create and initialize one more instance variable of type Bird in TestClass. Use this variable in TestClass's main to
assign values to the members of Bird explicitly.

Question - Given the above exercise,

1. If this were a test question, what would be correct answers to the underlined section in 2?
2. I must be reading exercise 3 wrong, because it doesn't make sense that a Bird type variable in TestClass can be used to assign an explicit value to say the int i in Bird.

Thanks

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

1.

Code: Select all

class TestClass{
 static Bird b = new Bird();
}
2.

Code: Select all

 
class TestClass{
 static Bird b1 = new Bird();
 Bird b2 = new Bird();
 pubic static void main(String[] args){
    TestClass tc = new TestClass();
    tc.b2.i = 10;
    tc.b2.str = "str";
     ...
 }
}
"Assign values to the members of this Bird instance in TestClass's main", would be more clear.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

Ok I figured I was reading it wrong. It was assigning literals to Bird variables from TestClass, not assigning Bird type values from TestClass to variables in Bird!

As for question one, is there a JVM processing advantage in using a combine declare/initialize (your answer) rather than the way I did it?

static Bird l1;
static {l1 = new Bird();}

As for the rest of the exercise 3,
"Assign values to the members of second Bird using the first Bird. Print the values of the members of both the Bird objects."

I probably am nitpicking now, but since the 2nd Bird object has been initialized, shouldn't the above read,

"Assign values to the members of first Bird using the second Bird. Print the values of the members of both the Bird objects."

for example using your code,

b1.i = tc.b2.i;

Thanks

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

OCAJO1 wrote:
Wed Jan 16, 2019 1:08 pm
As for question one, is there a JVM processing advantage in using a combine declare/initialize (your answer) rather than the way I did it?

static Bird l1;
static {l1 = new Bird();}
No, I don't think there is any advantage/disadvantage from JVM perspective. But I would prefer Bird l1 = new Bird(); because it clearer to read.
As for the rest of the exercise 3,
"Assign values to the members of second Bird using the first Bird. Print the values of the members of both the Bird objects."

I probably am nitpicking now, but since the 2nd Bird object has been initialized, shouldn't the above read,

"Assign values to the members of first Bird using the second Bird. Print the values of the members of both the Bird objects."

for example using your code,

b1.i = tc.b2.i;

Thanks
Yes, that would make more sense but the objective, I think, was to focus on accessing static and instance variables from a static method. So, b1.i = tc.b2.i; or tc.b2.i = b1.i; is not of much relevance.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

That's true. Need to instantiate or need not to instantiate, that is the question :)

Well that gets me to the last two questions about this set of exercises,

1. Add an instance variable of type Bird in Bird. Initialize this variable on the same line using
"new Bird()" syntax. Instantiate a Bird object in TestClass's main and execute it. Observe
the output.

I figured he was going for stackoverflow and that's what happened, but I want to understand what is it that causes the run time exception to be attributed to the Bird b = new Bird() in the Bird not in TestClass' main()?

2. Remove the initialization part of the variable that you added to Bird in previous exercise.
Initialize it with a new Bird object separately from TestClass's main. Identify how many
Bird objects will be garbage collected when the main method ends.

Considering the first set of Bird type variables (one instance and one static) called for at the beginning of the exercises and one in this section, it looks to me that when TestClass' main() ends, 3 Bird objects are gc ready. Is that correct?

Thanks

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

1. Object construction is done in a separate thread. But this would be way out of OCAJP scope.
2. What is your final code? Remember if an object is pointed to by a static and instance variables of a class/instance then that object cannot be GCed just because the main method ends.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

1. Ok starting from the exam scope, was the stackoverflow caused due to recursion call effect between the instantiation in the TestClass' main() and Object creation in Bird? If so, does that mean the thread constructing the Object in Bird, executes after the thread instantiating in the TestClass' main() causing the stackoverflow?

2. Oh, your point bring up another question. When ending of main() method means the end of the program - like in this exercise, don't the class objects pointed to at the class level lose their variable points as well, becoming gc eligible?

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

1. Stack overflow was caused due to unending recursion involved in the creation of Bird object. I am sorry, I didn't mean "a separate thread" from the program perspective. If you print the thread name from main and the thread name from Bird constructor, you will see the same name. I was talking from the perspective of the JVM. It is not the stack of the thread that is executing main (i.e. the main thread) that is getting filled up. Only one variable b is being put on main thread's stack. But at some point, the JVM has to allocate space for the Bird object, and, I am guessing, it is being done separately from this main thread using a separate stack, which is why the StackOverflowError is not associated with the main thread.
But I will admit that I do not know for sure the exact mechanism though which it is happening. May be if you look at the byte code generated for this class, you will get more clarity.

2. GC depends on whether an object is being pointed to by valid reference in an active thread. static variables belong to the class and objects pointed by static variables will be eligible only when the class is unloaded. Now, does a class get unloaded if a program ends? Probably. But unloading of a class is not a part of Garbage collection. This is too complicated an area to be covered in the exam. For the exam, you need to focus only on the objects pointed to by local variables and what happens to them when the method ends.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

I keep being amazed every time I find out how much more complicated Java is when looking behind what a programmer sees!

Also, when you say "Stack overflow was caused due to unending recursion involved in the creation of Bird object", was it because of back and forth between the creation of Bird Object in Bird, and instantiation of Bird Object TestClass' main()? I think I'm not understanding (or missing) something basic again, so please feel free with elaborating Java 101 in this matter:)

Thanks

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

Yes, while creating a Bird object, the JVM has to create another bird object first to set the instance variable.
It's like to make a dollar, you need a dollar first. So, there is only "forth" and no "back" :D
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

These are excerpts from exercise 7, 10 & 11 from chapter 4.

7. Define a simple class named Data with a public instance field named value of type int.
Create and initialize a Data variable named d in TestClass' main. Create an array of Data
of length 3 and initialize each of its elements with the same Data instance. Use any of the
array elements to update the value field of the Data object.

Question,

Given that the value field is of type int and the fact that d variable has to be of type Data to satisfy the initialization of the array of Data, how does the last sentence play out?

10. Declare and initialize a variable of type array of Objects of length 3. Initialize the first element
of this array with an array of ints, second with an array of array of ints, and third with an
array of Objects. See which one of the assignments fails compilation.

I initialized using the following,

Object[] oa = new Object[] {new int[1], new int[1][],new Object[1]};
and
Object[] oa1 = {new int[1], new int[1][],new Object[1]};

Did not get any compiler error or Exceptions on either one. So what happened?

11. Given the statement int[][] nums = new int[1][3];, how many int values can nums
store? Write down how each element of nums can be addressed.

Obviously the answer to both questions is 3. Unless this is some sort of exam type wording trick :shock:

Thanks

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

1. arr[0].value = d.value;

2. Correct, none of the assignments should fail. I think "one" in the question is causing confusion. Should be fixed.

3. 3 is correct.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

Oh wow, I must have totally blanked out on the fact that each element of the array of Data type initialized in TestClass' main() can be just as well as variable d, a reference to Data class' variable - value!

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

I was looking at part of the 1st exercise of chapter five,

int m = 50;
int n = ++m;
int o = m--;
int p = --o+m--;
int x = m<n?n<o?o<p?p:o:n:m; //note: for some reason this full editor is showing colon o colon as a big pink/light red o!

or

int x = m<n?(n<o?(o<p?p:o):n):m;

or

int x1 = o<p? p:o;
int x2 = n<o? x1:n;
int x3 = m<n? x2:m;

Question,

How do I go about converting a three step ternary to if-else statements?

Thanks

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

A ternary expression is of the format :
booleanExpression ? expression1 : expression2;

m<n?n<o?o<p?p:o:n:m; will be grouped as :
m<n? (n<o?(o<p?p:o):n):m;

You can now easily visualize it as an if-else:
if(m<n){
(n<o?(o<p?p:o):n)
} else{
m;
}

But an if-else statement is not an expression. It does not have a value of its own that can be assigned to a variable. So, an exact conversion of ternary to if-else is not possible.
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by OCAJO1 »

Oh I should have been more clear. What I am after is - I've seen examples on various internet sites that on this exam, either the questions come up with showing a ternary and asking to choose the equivalent if-else or visa versa. So I was wondering if there is some sort of correlation (for the exam purposes) that one can look for, when such questions arise?

admin
Site Admin
Posts: 9010
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by admin »

No, I don't think there is any single correct way to convert. There are always multiple ways to do a thing.
If you like our products and services, please help us by posting your review here.

BlackCat
Posts: 8
Joined: Wed Jan 29, 2020 11:09 am
Contact:

Re: [HD Pg 374, Sec. 2.8.0 - exercises]

Post by BlackCat »

I don’t understand what this means from Section 3.7 exercises of the Java 8 associate fundamentals book.

6. Remove the initialisation part of the variable that you added to Bird in previous exercise.
Initialise it with a new Bird object separately from TestClass’s main.

I get the first part - I got an exception when I initialised the Bird variable in Bird with new Bird(). So clear on removing that. But how do you then initialise that instance variable of type Bird in Bird “separately from TestClass’s main”?

Does this mean you should create a constructor in the Bird class?

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests