• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

another set of codes

 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can you change the break statement below so that it breaks out of both the inner and middle loops and continues with the next iteration of the outer loop?

outer: for (int x = 0; x < 3; x++) {
middle: for (int y = 0; y < 3; y++) {
inner: for (int z = 0; z < 3; z++) {
if (arr(x, y, z) == targetValue)
break;
}
}
}


class Test {
public static void main(String[] args) {
Test t = new Test();
t.test(1.0, 2L, 3);

}
void test(double a, double b, short c) {

System.out.println("1");
}
void test(float a, byte b, byte c) {

System.out.println("2");
}
void test(double a, double b, double c) {

System.out.println("3");
}
void test(int a, long b, int c) {

System.out.println("4");
}
void test(long a, long b, long c) {

System.out.println("5");
}
}

whats teh op



Which of the following java classes are immutable??




A.java.lang.Math

B.java.util.String

C.java.util.StringBuffer

D.java.lang.Boolean

E.java.lang.Object

Question 11.

Which of the follwing is true about static modifier.




A.static can be applied to : instance variables, methods,
code Segments and classes.

B.a static method cannot be overridden.

C.inner classes can be both static & private.

D.a static reference cannot be made through non static
method or code block.

E.abstract & static both can be applied together to a
method.
 
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I guess you could use a label, declared just before your 3 loops, and break out of the label.




void test(double a, double b, double c) is the method called, so 3 is printed.

take the call t.test(1.0, 2L, 3);
- 1.0 is a double
- 2L is a long
- 3 is an int

you know the double cannot be implicitly cast to another primitive.

so you have to choose between
- "void test(double a, double b, short c)"
- "void test(double a, double b, double c)"

then, you know that your int cannot be implicitly cast to short.

so you choose "void test(double a, double b, double c)"





Which of the following java classes are immutable??

A. java.lang.Math

B. java.util.String

C. java.util.StringBuffer

D. java.lang.Boolean

E. java.lang.Object



Uhm... I would say B and C.






Which of the follwing is true about static modifier.

A. static can be applied to : instance variables, methods,
code Segments and classes.

B. a static method cannot be overridden.

C. inner classes can be both static & private.

D. a static reference cannot be made through non static
method or code block.

E. abstract & static both can be applied together to a
method.


I would say A, B, C.


bye,
Alex
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alex Turcot:
Hi,

I guess you could use a label, declared just before your 3 loops, and break out of the label.


I disagree. If you did that, you would break out of all 3 loops, and you would not continue with the next iteration of the outer loop, as the question specified.

What would work in this situation is if you changed the break statement to either one of the following:
break middle;
OR
continue outer;


Which of the following java classes are immutable??

A. java.lang.Math

B. java.util.String

C. java.util.StringBuffer

D. java.lang.Boolean

E. java.lang.Object



Uhm... I would say B and C.



StringBuffer is not immutable, since the data stored in a StringBuffer instance can be altered.

An immutable class is one from which you can create an instance that holds data, where you cannot alter any of the data after the instance has been created. The immutable classes that matter for the exam are String and the wrapper classes Boolean, Byte, Short, Character, Integer, Long, Float, and Double.

Math and Object are not considered immutable classes, as far as I know, because you cannot make instances of them which hold data that cannot be altered.

I believe the answer is B and D.



Which of the follwing is true about static modifier.

A. static can be applied to : instance variables, methods,
code Segments and classes.

B. a static method cannot be overridden.

C. inner classes can be both static & private.

D. a static reference cannot be made through non static
method or code block.

E. abstract & static both can be applied together to a
method.


I would say A, B, C.



A is false because static cannot be applied to instance variables. If a member variable is static, then it is not an instance variable.

B is true, because according to JLS 8.4.6.2,

If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.


Static methods can hide one another, but they cannot override one another.

C is true, because this code compiles:


D is awkwardly worded (it should say "in a non-static method", not "through non-static method"), but it's true. You can only make a static reference where it will be a class variable or an interface variable, outside of any methods or code blocks. You cannot make a static reference in any method or code block. That includes static methods, static code blocks, non-static methods, and non-static code blocks. Here's some code that attempts to make static references in those illegal places, causing a compiler error in each illegal case:


E is false. A method cannot be both abstract and static.

So the answer is B, C, and D.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

You are right for the label, it misread - continues with the next iteration -

I see what you mean with - static can be applied to : instance variables, methods, code Segments and classes.

its really picky though, are you sure about this... cause the way I read this, lets say you have an instance variable, you can apply the keyword static to it.. it wont be an instance variable anymore, but you can.

do you see my point. But I guess you are right, an instance variable cannot hold this modifier.


For this...
D. a static reference cannot be made through non static
method or code block.

Again, I understood that you could not refer to a static variable from a non-static method, which would be false. I really dont like this question.. "make", "through"..

Thanks for all your precision,
Alex
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be totally clear for anyone reading this post, you can refer to a static variable from a non-static context, like in this example:


What makes choice D confusing is its poor grammar. Some mock exams have poorly written questions and answers like this. It might mean "a static reference cannot be used in a non static method or code block" which is false, or it might mean "a static reference cannot be declared in a non static method or code block" which is true. Given that the writer chose the verb "made" my best guess is that they meant "declared". Using "through" and leaving out "a" just shows that the writer was not very good at English, which is a shame when absolute precision is paramount for writing a mock exam question.

As for being really picky about applying static to instance variables, yes it is very picky, and so are a lot of the exam questions. I can see your point of view, and I agree that "static can be applied to : instance variables, methods, code Segments and classes" is poorly worded since it could be interpreted in more than one way. Maybe the writer thought it was true and just didn't think much about it. Since by definition "static" does not apply to instance variables, it doesn't seem right to say that it "can be applied" to instance variables. That is, unless the writer is implying that you start with code that has an instance variable, then you add "static" to it so it is no longer an instance variable. If that's what the writer meant, then I consider this answer choice to be very poorly worded. If they had said "variables" instead of "instance variables" then choice A would definitely be true. However, they specified "instance variables", so the complete meaning of the statement is not abundantly clear to diverse readers.

Hopefully there won't be any ambiguous questions or answer choices on the real exam. However, I do expect it to be extremely nit-picky about seemingly unimportant details. That's part of what makes the exam challenging.
 
Alex Belisle Turcot
Ranch Hand
Posts: 516
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the details
 
Parameswaran Thangavel
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all
can i say in nutshell

The static reference can't be created or declared in static method,static block and non-static method,non-static block.

But it can be used at anywhere by simply using its reference variable..

ANd

Continue label;---> will go for the next iteration of the loop placed under the label;
break label;------> will just get the control out of the loop placed under the label;


is it ok...
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Parameswaran Thangavel:
hi all
can i say in nutshell

The static reference can't be created or declared in static method,static block and non-static method,non-static block.

But it can be used at anywhere by simply using its reference variable..

ANd

Continue label;---> will go for the next iteration of the loop placed under the label;
break label;------> will just get the control out of the loop placed under the label;


is it ok...



Yes, yes, yes, and yes. You got it.
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic