(2) Algorithms I Flashcards

1
Q

Definition of Algorithm

A

A procedure for a systematic and stepwise problem solution.

  • abstracts from concrete implementation
  • computational procedure that takes input and produces an output
  • well defined execution order
  • solves a class of problems (not a single problem)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Characteristics of algorithms

A
  • independent of particular programming languages
  • independent of particular software
  • creative: different algorithms can solve a problem
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can we transfer an algorithm to machine language?

A

Compiler or Interpreter

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

Definition Compiler

A
  • computer program
    transforms complete program in a programming language in a program in machine language
  • object code can be executed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How does a Compiler work?

A
  • Scans the entire program first, then translate the entire program and executes the translated program
  • Source code is translated exactly once
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Definition Interpreter

A
  • Computer program

- Takes one statement at a time of a programming language and translates it into machine language

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

How does an Interpreter work?

A
  • Scans one statement at a time and translates it
  • Directly executes every translated statement
  • Source code might need to be translated several times
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are pros and cons of a Compiler and an Interpreter?

A

Compiler:
+ more efficient -> only one translation of entire program necessary
+ program may be optimised after translation
- for every source code change, program has to be entirely retranslated

Interpreter:
+ fast Response to changes in the program
- repeated translations of statements
- execution is very time consuming

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

How are programs and data fundamentals designed in Java?

A

Program: Statements

  • number of statements
  • represent flow of control
  • operate data

Data: Variables

  • refer to memory addresses where data is stored
  • symbolic identifiers commonly used
  • variables are given by their name
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Variable Definition

A

A variable is a symbolic reference to some memory cell. An alias (name) is used to address the variable.

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

What is a constructed Java type?

A
  • List of same-type elements (array)

- Combination of different types (record)

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

Definition Array

A

Collection of elements of the same data type.

  • array element is accessed via its index
  • arrays may be multi-dimensional
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the default values of an int array and string array?

A

int: default value is 0
string: no default value (empty fields)

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

An algorithm is a sequence of steps with…

A
  1. No parallel execution of steps.
  2. Every step is executed exactly once. No neglection, no repetition.
  3. The execution order corresponds exactly to the order defined by
    the statements.
  4. The execution of the last step of the algorithm completes the algorithm.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Definition Control Structure

A

A control structure is an instruction which allows to manipulate the order of execution such that the flow differs from the implicitly specified sequential order (restricted to a single thread of execution).

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

Definition Conditional Statement

A

A conditional statement defines that a condition must evaluate to true in order to proceed with the execution of the subsequent statement

17
Q

Definition Loops

A

Loops allow the execution of a statement or block of statements repeatedly.

18
Q

Definition For-Loop

A
  • A for-loop consists of a loop head and a loop body
  • The loop head is a control expression which consists of three parameter:
    1. Initialization expression
    2. Loop test expression
    3. Iterator

for (int i = 1; i <= 6; i++) {
j = j + i;
}

19
Q

Definition While-Loop

A
  • The while loop executes its body as long as the loop condition is met
  • The loop expression is tested at the beginning of the loop

while (i <= 6) {
j = j + i;
i++;
}

20
Q

Definition Do-While-Loop

A
  • The do-while loop executes its body as long as the loop condition is met
  • The loop expression is tested at the end of the loop (body is executed at least once)

do {
j = j + i;
i++;
} while (i <= 6);

21
Q

Definition and Examples of Infinite Loops

A
  • In most cases, infinite loops are caused by incorrect termination conditions
  • Non-terminating algorithms are not correct if they require infinite time to solve a problem
  • Non-terminating algorithms can be correct if they continuously solve a permanently recurring problem:
    • traffic light regulation
    • artificial respiration

while (i < 6) {
i = i – 1;
x = x * x;
}

22
Q

In every loop we must make sure that:

A

(1) the control variable is initialized
(2) its value is changed in every run
(3) it is used in the loop condition
(4) it eventually reaches a value that the loop condition will evaluate to false

23
Q

Definition of Method

A
  • help to structure our programs/algorithms
  • provide reuse of functionality
  • encapsulate the complexity of the algorithm
24
Q

Composition of a Method

A
  • The data type of the returned result (returnType)
  • The name of the method (methodName)
  • A list of formal parameters (type1 arg1, type2 arg2, …)
  • The method body (the actual algorithm)
25
Q

In which case is an algorithm recursive?

A

An algorithm is recursive if it calls itself

int factorial (int number) {
   if (number <= 1) {
        return 1; 
   }
   else {
        return number * factorial(number - 1); 
   }
}