Introduction to Java
🇬🇧
In English
In English
Practice Known Questions
Stay up to date with your due questions
Complete 5 questions to enable practice
Exams
Exam: Test your skills
Test your skills in exam mode
Learn New Questions
Manual Mode [BETA]
Select your own question and answer types
Specific modes
Learn with flashcards
Complete the sentence
Listening & SpellingSpelling: Type what you hear
multiple choiceMultiple choice mode
SpeakingAnswer with voice
Speaking & ListeningPractice pronunciation
TypingTyping only mode
Introduction to Java - Leaderboard
Introduction to Java - Details
Levels:
Questions:
44 questions
🇬🇧 | 🇬🇧 |
A variable consists of | 1. Address 2. Type / Data type 3. Value e.g int number = 1; |
What is a Literal? | A data value that appears in the program text without a variable e.g A number 5 or 3.6, or a text: "This is a text literal" |
What is a constant? | A variable whose content cannot be changed - e.g., pi for 3.141592653589793 |
In a high-level programming language, the instructions (statements, commands) are independent of the processor type. | Evaluation of expressions (calculations) and value assignment (e.g. x ← 10 * 6 + 7) Command sequence* Branching* (conditional statement, selection, choice) – two-way or multiway Loop* (iteration, repetition) – head-controlled or foot-controlled Subroutine call |
What are the types of branching? | Two-way branching Selection of the next statement(s) based on a logical expression (e.g if else) Multiway branching Selection of the next statement(s) based on an expression with finite value range (e.g switch case) |
What are the types of repititions? | Head-controlled loop Repeat as long as a condition checked at the loop entry is fulfilled (e.g while -> statement) Foot-controlled loop Repeat as long as a condition checked at the loop exit is fulfilled (e.g statement -> while) |
What are comments? | Comments are used to explain the program Nesting of multiline comments is not allowed. |
Notation for production rules in EBNF | A ::= x y z concatenation xyz A ::= x1|x2|...|xk alternative x1 or x2 A ::= x [y] z option xz or xyz A ::= x {y} z repetition xz or xyz or xy or yz A ::= x {y}+ z repetition y at least once (xz is forbidden) A ::= (a | x) (b | z) bracketing of text parts ab or xb or az or xz |
Syntax of a name (identifier) in Java | Identifier ::= (Letter | SpecialSymol ) or { Letter | Digit | SpecialSymbol } |
Riddle | WHICH ALGEBRAIC LAW FOR THE OPERATORS && AND || IS VIOLATED BECAUSE OF THIS? |
Riddle | WHICH ALGEBRAIC LAW FOR THE OPERATORS && AND || IS VIOLATED BECAUSE OF THIS? |
Riddle | WHICH ALGEBRAIC LAW FOR THE OPERATORS && AND || IS VIOLATED BECAUSE OF THIS? |
Riddle | WHICH ALGEBRAIC LAW FOR THE OPERATORS && AND || IS VIOLATED BECAUSE OF THIS? |
Riddle | WHICH ALGEBRAIC LAW FOR THE OPERATORS && AND || IS VIOLATED BECAUSE OF THIS? |
What does a variable consist of? | Data type - int, double, String Identifier (name) - •Combination of letters, digits and special characters (without spaces etc. and operators like +, -,*, etc.) •Must start with a letter •Identifiers are case sensitive •Java keywords are not allowed (e.g. void, int) •Convention for compound names: myName |
Does variable have initial values? | Initially, variables have no well-defined value (not even 0!), but can have arbitrary (possibly also invalid) contents. |
Floating point numbers (float, double) | Literals: • 0.23, -47.11 • Scientific notation: -1.23E-45 (= -1,23x10-45) • In principle double; float can be forced by a suffix f or F Operations: • additive: +, - (binary and unary) • multiplicative: *, / • comparison: == (attention – often imprecise!), !=, >, <, >=, <= |
Logical values (boolean) | Literals: • true or false Operations (boolean algebra): • Negation: ! (unary) • Conjunction (∧, AND): && • Disjunction (∨, OR): || • Antivalence (≢, XOR): ^ • Comparison: ==, != |
Characters (char) | For storing individual characters of a text: •Upper and lower case letters, digits, special characters •Value range: 216 possible characters (Unicode) Literal: between ' ' Comparisons (lexicographic): ==, !=, >, <, >=, <= • +, - with whole numbers (»code arithmetic«): 'A' + 1 ➝ (Unicode of A) + 1 ➝ Unicode of B No possibility of input via a Scanner variable |
Character Strings (String) | For storing texts. Actually not a primitive data type, but a class Input via a Scanner variable in: in.next() |
Expressions | Expressions are combinations of operands and corresponding operators. operators: +, -, *, /, % operands: x, y, z, 1, 2, 3 |
Type compatibility & Type conversions (1) | Operands of an expression must be (data) type compatible to each other – each operation (+, *, ...) ultimately requires operands of the same data type. Two types are compatible if they are • equal, or • one can be converted to the other by implicit type conversion (performed autonomously by the compiler). Implicit (automatic) data type conversion (type promotion) • byte → short → int → long → float → double • char → int • All primitive types → String These conversions never cause data loss! |
Type compatibility & Type conversions (2) | If no implicit type conversion is possible, an explicit conversion must take place. Data loss is possible in this case. e.g int area = (int) (r*r*3.14159); |
Conversion of strings to primitive types | Int valueI = Integer.parseInt("123"); double valueD = Double.parseDouble("123.4"); |
Assignment as an expression | Assignments are actually expressions expression: result = a + b * c assignment: = |
Shortcut assignment operators | Compund assignment operators following the pattern •= (for any binary operator •): a += b; // is equivalent to a = a + b; a -= b; // is equivalent to a = a - b; a *= b; // is equivalent to a = a * b; a /= b; // is equivalent to a = a / b; |
Evaluation order (1) | Subexpressions are evaluated (with respect to the run-time) strictly from left to right. Logical expression are »lazyly« evaluated (conditional evaluation, lazy evaluation) – only as long as the final result is not yet determined: • x && y yields always false, if x == false • x || y yields always true, if x == true Therefore, y is not evaluated in these cases |
Evaluation order (2) | The order is especially important if side effects occur in the expression! Example 1: int i = 2; int j = (i=3) * i; // j = 9 |
Riddle | WHICH ALGEBRAIC LAW FOR THE OPERATORS && AND || IS VIOLATED BECAUSE OF THIS? |
Conditional statements | Two-way-branching - if else Nested-branching - if else within if else Multiway-branching - switch case |
Repitition statements | While loop (Head controlled) For Loop (Head controlled) Do Loop (Foot controlled) |
Types of jump statement | Break - exits the immediately surrounding loop or switch statement. continue (only in loops) - exits the current pass of the immediately surrounding loop and continues at the loop head. assert (for checking assertions) - Systematic testing of assertions helps to detect programming errors |
Riddle | WHAT IS THE LARGEST n FOR WHICH A CORRECT RESULT CAN BE EXPECTED? |
What is a subroutine? | Parts of a program can also be formulated elsewhere as a subroutine (subprogram), where the control flow • branches there if required (call) and • returns to the place of the call after the subroutine has terminated (return). We use subroutines to • factor out common parts of an algorithm for reuse • structure larger programs in »digestible units« (modules) |
What are parameters? | Parameters are means to communicate with subroutines: • In the header, formal parameters are defined, which • act like local variables within the subroutine, but are • initialized by an actual parameter (value of an expression) each time the subroutine is called |
Method overloading | Method names can occur multiple times as long as the methods with the same name differ sufficiently by number or data types of the formal parameters:Naming different things (usually methods) with the same name is called overloading.In a method call, the ambiguity (»Which method is meant?«) is resolved based on number and types of the actual parameters. |
Riddle | RIDDLE IMPLEMENT THESE CHANGES Extend the circle program by adding another for-loop around the circle drawing loop, which runs, e.g., ten times. Starting with a value of, e.g., 0.1, after each circle segment, slightly enlarge the radius. |
Riddle | RIDDLE IMPLEMENT THE VERSION WITH TRACE |
What are the key factors of structured programming? | • Simple building blocks whose individual correctness can be easily proven •Well-defined rules that enable correct assembly of these building blocks |
Structured programming - Rule of thumb | •No »free« jump statements (»Goto considerd harmful« – Edsger Dijkstra, 1968) •Each loop has exactly one entry and exactly one exit point (➝ no break) • Statements should have exactly one effect (no »side effects«) • In multiway branches (switch) always exactly one path should be traversed (➝ no sequential continuation, always specify default clause) |
Structured programming - Rule of thumb (2) | • Subroutines communicate with each other exclusively via parameters and function values (or via class and instance variables – see later) and not via shared, global data structures • A subroutine should also be documented as precisely as possible (ideally formally), in particular: • the effect mentioned above (»postcondition of the subroutine«) • the conditions that must be met for the subroutine to be able to achieve this effect (»precondition of the subroutine«) |
Array | Arrays have (in general and in Java) fixed lengths: They contain – once created – a fixed number of data elements. Variables that manage arrays are reference variables (pointer variables) in Java: They contain the starting address of the memory area in which the individual array elements are stored one after the other. Those are accessed by specifying the element number (indexing): |
How to create an array? | Via Initialization: int a[] = new int [20] via assignment: int b; b = new int[] {0,8,15, 47, 11}; |
Riddle | RIDDLE EXTENSIONS: 1. EXPAND PROMPT TO »NUMBER i OF n:«. 2. PRINT THE ARRAY IN REVERSE ORDER 3. PRINT THE ARRAY IN BOTH ORDERS (3-COLUMN LAYOUT: 1ST COLUMN: PLACE NUMBER (INDEX) i, 2ND COLUMN: iTH VALUE IN INPUT ORDER, 3RD COLUMN: iTH VALUE IN REVERSE ORDER) 4. CHANGE ARRAY TO A FLOATING POINT ARRAY WITH OUTPUT OF DESCRIPTIVE STATISTICS (SUM OF VALUES, MEAN, VARIANCE...) |