Code Formatting on the Exam Flashcards

1
Q

How should interpret a code snippet in the exam that does not start with number one ?

A

You’ll see examples with line numbers that don’t begin with 1 in this case. The question is telling you, “Don’t worry—imagine the code we omitted is correct; just focus on what I’m giving you.”

This means when you
do see the line number 1 or no line numbers at all, you have to make sure imports aren’t
missing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give an example for each one !

A

EXAMPLE 1 :

You should expect to see code like the following and to be asked whether it compiles :

6: public void method(ArrayList list) {
7: if (list.isEmpty()) { System.out.println(“e”);
8: } else { System.out.println(“n”);
9: } }

EXAMPLE 2 :

1: public class LineNumbers {
2: public void method(ArrayList list) {
3: if (list.isEmpty()) { System.out.println(“e”);
4: } else { System.out.println(“n”);
5: } } }

For this one, you would answer “Does not compile.” Since the code begins with line 1,
you don’t get to assume that valid imports were provided earlier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How should you interpret a code snippet that does not have a main method or any class structure ?

A

You’ll also see code that doesn’t have a main() method. When this happens, assume
the main() method, class defi nition, and all necessary imports are present. You’re just
being asked if the part of the code you’re shown compiles when dropped into valid surrounding
code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly