Examination

There will be a written examination at the end of period 2. During the exam, it is allowed to use the course book (Horstmann: Java For Everyone). If you prefer, you can also use the Swedish book “Skansholm: Java Direkt”. In order to pass the course, you must:

Your score for the course is determined by the final exam. The exam is done using pen and paper, not on a computer.

Examination dates

See this page for more details.

Frequently asked questions

Q: Will the exam be in English or Swedish?

This year the exam questions will be in English. It is preferred that you answer in English, but you are allowed to answer in Swedish if you are uncomfortable with English. You can also bring an English-Swedish dictionary to help you to understand the questions.

Q: Will the exam be similar to the exams from previous years?

The structure of the exam will be mostly similar to previous years: some questions where you get a piece of code and you have to answer some questions about it and/or correct the mistakes, and one or two questions where you have to write some code yourself on paper. For the latter kind of question, logical flaws are considered more serious than syntax errors.

What to study from the book

During the exam, you are allowed to use the course book (Java for Everyone: Late Objects, second edition). Marking and light notes are permitted, but you shouldn’t write anything longer than a few words.

Some general notes on the book:

Chapter 1: Introduction

(see also lecture 1) whole chapter

Chapter 2: Fundamental Data Types

(see also lecture 1) whole chapter except for:

Chapter 3: Decisions

(see also lecture 2) whole chapter except for:

Chapter 4: Loops

(see also lecture 3) whole chapter except for:

Chapter 5: Methods

(see also lectures 4 and 5) whole chapter

Chapter 6: Arrays and Array Lists

(see also lectures 6 and 7) whole chapter except for:

Chapter 7: Input/Output and Exception Handling

(see also lectures 8 and 13) whole chapter except for:

Chapter 8: Objects and Classes

(see also lecture 10) whole chapter except for:

Chapter 9: Inheritance and Interfaces

(see also lecture 11) whole chapter except for:

Chapter 10: Graphical User Interfaces

(see also lecture 12) whole chapter except for:

Chapter 11: Advanced User Interfaces (online chapter)

(see also lecture 12) ONLY the following parts:

Chapter 12: Object-Oriented Design (online chapter)

(see also lecture 14) ONLY the following parts:

Chapter 13: Recursion (online chapter)

(see also lecture 14) ONLY the following parts:

Chapter 14: Sorting and Searching (online chapter)

not required for the exam

Chapter 15: The Java Collections Framework (online chapter)

(see also lecture 11) ONLY the following parts:

Old exams

Below you can find the old exams for this course, as well as possible solutions (in Swedish):

FAQ for the old exams

Q: In EXAM 170109 exc. 1b they do put in the signaturs length() and chartAt(int i) in the table compared to EXAM 180113 where they aren’t. How come?

Calling .length() on a String or an ArrayList is a method call, but taking .length of an array is a built-in operation of Java (because arrays are not a class, they also don’t have methods). You can easily see the difference because a method call will always contain parenthesis ().

Q: In EXAM 170109 exc. 1c they exclude row 3, why?

A statement (such as for example a variable declaration “int i, j;”) is not part of the control flow of a program, the control flow only includes control statements (if, while, for, break, …) and method calls.

Q: In EXAM 170109 exc. 1d I do not understand how they could get that outcome?

If you don’t understand how the code works, it may help to run it on your own computer and add print statements so you can better understand what is happening during the execution of the program.

Q: In EXAM 180406 exc. 1e could you describe what is happening when they do change from int to Integer, and how to think?

The point is that the primitive type int is replaced by the class Integer. Because of automatic boxing and unboxing, this normally doesn’t require any changes to the program. However, the default value for the int type is 0, while the default for Integer is null (as for all classes). So when you create a new array of type Integer[], you have to manually set all values to 0.

Q: In EXAM 170109 exc. 1e why is the List depending on an Integer?

The method must return a list of integers for which the length is not yet known; the easiest way to do this is by using an ArrayList. However, ArrayList only works for classes and not for primitive types, so you should use ArrayList instead of ArrayList (which would not be valid Java code).

Q: In EXAM 180406 exc. 2, why do int k have to be initialized by = 0, couldn’t it only be int k; ?

No, in Java variables must be initialized (= be given a value) before they are used. The default value for a type is only used when creating an empty array of a given length (since it is not always possible to give all the values at once).

Q: In EXAM 170109 exc. 3a. How could the outcome be like that? I thought it would be something like 2, 3/2, Division be zero! osv.

Dividing an integer by another integer will always round the result down to the nearest integer, so for example here 3/2 is rounded down to 1. If you don’t want to round down, you must first convert at least one of the numbers to a floating-point number, for example ((double)3)/2.

Q: In EXAM 180406 exercise 5b. What does it mean when you write the expression ((D)e).put(3)?

((D)e).put(3) means two things: first you cast the variable e (which has type E) to type D, and then you call the method .put(3) on the resulting object of type D. But actually this type cast does not have any effect: it still refers to the same underlying object, which has type E. So the definition of put from class E will be used.

Q: In EXAM 180406 exc. 5, How could row 3 return 3, when it is missing a e.get()? Doesn´t it have to be ((D)e).get() in order to take it from the D class?

E is a subclass of D, so if you call a method on an object of type E and that method is not defined in E, then it will use the definition of that method from class D (this is how inheritance works in Java).

Q: In EXAM 180406 exc. 5, How could row 9 return 3 when g is missing a put instance? In general in these type of questions how to think? Is it right when I do think of ((D)f).getX() as f is going to D and in there looking for a getX, and are not taking with any values from f?

Again, if a method is missing from a class then Java will use the definition of that method from the superclass, which is F in this case.

Casting an object to a different type only impacts which methods are available, but not which implementation of an overridden method will actually be used. For that Java will simply ignore all type casts and simply look at the type of the object itself. In this example, since f has type F, writing ((D)f).getX() will still use the definition of getX from the class F, not from class D.

Q: In EXAM 170109 exc. 5, How come row 6 will return value 2 and not number 1 as row 3? What are the difference?

For this question, it is important that you first notice that an object of type B has two attributes: the value defined in class A (which can be accessed as super.value) and the value from class B itself. On line 6, the typecast in ((A)b).getVal() actually does not have any effect since the type of the object itself is still B, so the implementation of getVal from class B will be used.

Menu