Java maybe, some help with something puzzling

P

Poindexter

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
 
M

Mark Jeffcoat

Poindexter said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top