Java maybe, some help with something puzzling  
Author Message
Poindexter





PostPosted: 2006-10-11 9:52:00 Top

java-programmer, Java maybe, some help with something puzzling Hi all,
I was given this quiz as it were. I'm asked certain questions about
two snippets of code. The questions do not indicate the programming
language. I'm mainly familiar with java methods but don't know if other
languages would also use "methods." I'm also a bit confused because an
Array, which is indicated in the code below, should have the square brackets
[] and yet this code snippet is lacking that. Any advice on what I might
want to look at to understand this code further would be
greatly appreciated. Sorry if I seem ignorant here.
1. Consider the following method:

private int wrongCode(Integer num){
if (num > 6){
return num - 5;
} else {
return num.parseInt();
}
}
What are the errors?

2. Consider the following methods:

public int run() {
List group1 = new ArrayList();
for (int i=0;i<3;i++) {
group1.add(i);
}
List group2 = this.makeNewList(group1);
return group2.size() - group1.size();
}
private List makeNewList(List list) {
List other = list;
other.remove(other.size() - 1);
return other;
}
What value is returned from the method run() after it is called?

Thanks,
bruce


 
Andrew Thompson





PostPosted: 2006-10-11 10:01:00 Top

java-programmer >> Java maybe, some help with something puzzling Poindexter wrote:
> Hi all,
> I was given this quiz as it were. ..

Give it back.

(Follow-ups set to comp.lang.java.programmer only)

Andrew T.

 
Mark Jeffcoat





PostPosted: 2006-10-11 17:08:00 Top

java-programmer >> Java maybe, some help with something puzzling "Poindexter" <email***@***.com> writes:

> Hi all,
> I was given this quiz as it were. I'm asked certain questions about
> two snippets of code. The questions do not indicate the programming
> language. I'm mainly familiar with java methods but don't know if other
> languages would also use "methods." I'm also a bit confused because an
> Array, which is indicated in the code below, should have the square brackets
> [] and yet this code snippet is lacking that. Any advice on what I might
> want to look at to understand this code further would be
> greatly appreciated. Sorry if I seem ignorant here.

Homework? Job interview? I'll give a few hints:

First, it's definitely Java.


> 1. Consider the following method:
>
> private int wrongCode(Integer num){
> if (num > 6){
> return num - 5;
> } else {
> return num.parseInt();
> }
> }
> What are the errors?

Have you tried putting this method into
a class and compiling? The compiler will
tell you quite directly what's wrong.

>
> 2. Consider the following methods:
>
> public int run() {
> List group1 = new ArrayList();
> for (int i=0;i<3;i++) {
> group1.add(i);
> }
> List group2 = this.makeNewList(group1);
> return group2.size() - group1.size();
> }
> private List makeNewList(List list) {
> List other = list;
> other.remove(other.size() - 1);
> return other;
> }
> What value is returned from the method run() after it is called?


ArrayList is a List, not an array (thus, no []).
It's backed by an array, which may give you some hints
about how it should perform. See the javadocs.

If you just want to know the value, you can just
ask Java. No problem. Really, though, this question
is asking you to think about the difference between
a reference, and the Object the reference points to.

--
Mark Jeffcoat
Austin, TX