KODSTIL - quiz Flashcards

1
Q

True or False?

The main comment can be written as:

 // This is the main comment
 // without author tag and author name
 // and the backslash like this is totally fine
A

False!

Exactly! Do not use // or /*. This is for normal comments, that span 1 line or several lines, respectively. Please, write the main comment like this

/**
 * Programmet beräknar laddtider för ett antal kombinationer av ström/spänning
 * 
 * 1. Skapa konstanter och variabler
 * 2. Beräkna laddtider
 * 3. Avrunda till två decimaler 
 * 4. Skriv ut resultat 
 *
 * @author Susanne Fahlman, susfah-8
 */
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False?

In the course style rules, it is written that we do not want to have “magic numbers”. This means you shall never have hard-coded numbers. So, I have to write numbers in letters, like

final int ZERO = 0;
final int ONE = 1;

A

False

Do not write numbers in letters. It makes absolutely NO SENSE.

Use constants for hard-coded numbers that have a very specific meaning in your code. For example

MAX_PRICE

CURRENT_LOW

EFFICIENCY_FACTOR.

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

True or False?

In the names of the variables and the constants, I can use åöä

A

False

Exactly. åöä are not allowed in the names of the variables or constants. However, you can use them in the printouts or for comments on your code, even thought English is preferred.

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

True or False?

Constants shall be written in CAPITAL_LETTERS, with a “_” between the words.

So, this is a constant

int MIN_PRICE = 0;

A

False

A constant is a constant when the keyword “final” is used

final int MIN_PRICE = 0;

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

True or False?

It is okay if I initialize a variable with a computation, like

int area = base * height;

A

False

Correct! Keep declaration/initialization and computation separate, to have a tidy-up code

//Declaration and initialization of constants
final int BASE = 3;
...
// Declaration of variables
int area = 0;

// Computations

area = BASE * HEIGHT;

// Printout

…..

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

True or False?

Global variables shall not be used, unless specifically required

A

True

Correct. Global variables are only allowed when explicitly required in the exercise description.

Global variables are normally discouraged in most programming languages.
Simply said, from a memory point of view, it is “quicker” for the program to use “local variables”, rather than looking in the memory again for your global variables. From an error perspective, there is a high risk to overwrite them.

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

True or False?

It is totally fine to comment a method with a generic comment, like this

// This method computes an area   
public static double area(int radius, int height)
   {
      double area = 0;      
      final double PI = 3.14;    
      area = PI * radius * height;
  return area;          }
A

False

Use the javadoc, like this

/**

* Method that computes an area   * @param - radius
* @param - height
* @return - area
* /
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

True or False?

In this course, we write variables starting with a small letter, and separating the different words with a big letter, like this

userInput

A

True

Correct. Instead, constants are written in CAPITAL_LETTERS (_ to distinguish between words).

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