• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two simple questions  RSS feed

 
Miles Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two simple Java homework questions that need answering. I need help not because I don't understand the concepts, but because the questions seems a little ambiguous in their wording, and I need help interpreting them.

Question 1:

Some class has an instance variable named hours. Where can the coding this.hours = 5 be used?

A) within any static method of the class
B) within any nonstatic method of the class
C) outside of every methods, as long as it is within the class
D) none of the above

We don't know if the instance variable is public or private, but I am assuming it is private because they usually are. If this is so, I would think that as long as the declaration was in the class, this.hours = 5 would be legal. But none of these answers seem to point to that.

Question 2:

Fill in the blank.

s.charAt(k): This methods returns the character at position ____.

"Position" here seems a little ambiguous. Position could mean our typical ordering system (e.g. 1, 2, 3, 4...) or it could mean Java's (e.g. 0, 1, 2, 3...). Therefore, it seems that the answer would either be k or k - 1, but I am not sure which.

If someone could give me some pointers on these questions I would really appreciate it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Question 1:

A. Wrong, because 'this' cannot be used in static methods.
B. Correct.
C. Wrong, because you cannot have arbitrary statements such as an assignment statement in the class body, outside of a method or constructor.
D. Wrong, because B is correct.

Question 2:

This one is indeed a bit ambiguous. It could be k or k + 1 (not k - 1), depending on whether you start counting at 0 or 1.

I would just write down: "k (counting from 0)".
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. An access modifier does not matter in this case as the expression is used inside the class.

2. I can't help with that one. You need to ask the author of the question.

Jesper de Jong wrote:C. Wrong, because you cannot have arbitrary statements such as an assignment statement in the class body, outside of a method or constructor.

... or initialization block.
 
Miles Davis
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, that really cleared things up. However, I have one more question, difficult because of its ambiguity and somewhat sloppiness.

Question 3:

The first number that this coding prints is _________________.

for (x = 1; x < 100; x*=2);
System.out.println(x);

System.out.println(x);

I wrote down this question exactly as it appears. However, there seems to be problems, like the fact that the variable x is never assigned a type, and that there is a semi-colon after the for statement. However, what would guys put down as the answer to this? 1?
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is 128 provided x is declared somewhere.
I think you can assume it is.

The colon after for is not a problem. It means "no expression".
It is an equivalent to:

you should never write such code as it looks very confusing.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!