StudierendeLehrende
  1. Universität
  2. Ludwig-Maximilians-Universität München
  3. Einführung in die Programmierung

Altklausur 17

Bearbeite Altklausur 17 und vergleiche deine Lösungen. Aus dem Kurs Einführung in die Programmierung an der Ludwig-Maximilians-Universität München (LMU).

Abschnitt 2

Freitextaufgabe
2
1 P

Prove the validity of the following Hoare-Triple using the Hoare calculus:
{x≥0r=0x ≥ 0^r=0x≥0r=0}c {r=x2r = x²r=x2}
where all variables i, r, x have the type int and c is the following program snippet:
int i = 0%;
while (i!=x) {
i++;
r = r + i;
r = i + r;
}
r = r - ✗%;
Write down the invariant of the while-loop after your proof:
.
x ≥ 0 ^ r = 2 · Σ³ ³±0 k ODER r = i ⋅ i(+1) ODER r − i = i²
k=0
•
Hints: The famous German mathematician Carl Friedrich Gauss found at the age of 9 the formula 1 k
for n Є N.

n.(n+1)
2.

Deine Antwort:

2
Fortsetzung von Aufgabe 1
1 P

Welche Ausgaben liefert folgendes Programm:
Katze jule = new Katze();
Canis wolf = new Canis(44);
Canis sammy = new Hund (12,sammy);
Hund pia = new Hund (16, pia);
Tier[] zoo = new Tier[] {jule, sammy, wolf, pia};
for(Tier t : zoo) { System.out.println(t.gibLaut()); }
System.out.println(Sammy vs Jule:

  • sammy.gewinnt (jule));
    System.out.println(Jule vs Wolf:
  • jule.gewinnt(wolf));
    System.out.println(Sammy vs Wolf:
  • sammy.gewinnt (wolf));
    System.out.println(Sammy vs Pia:
  • sammy.gewinnt (pia));
    sammy.eatPrey (jule);
    System.out.println(Sammy kg: +sammy.getKg());
    wolf.eatPrey (sammy);
    System.out.println(Wolf kg: +wolf.getKg());
Deine Antwort:

Abschnitt MAIN-0ca0bb5e-fc43-497d-86c5-fe9178d75851

Gemischt
Lösung Aufgabe 1 (UML & Vererbung)
12 P

Gegeben sind folgende Klassen:
public class Spielzeug {
public void play() {
System.out.println(Whee!);
}
}
public interface Tier {
public String gibLaut();
public default String gewinnt (Tier t) {
if(gibLaut().length() > t.gibLaut ().length())
return ja;
else return nein;
}
}
public class Katze implements Tier {
private Spielzeug spielzeug;
@Override
public String gibLaut () {return Miau;}
}
}
public class Hund extends Canis {
private String name;
public Hund(int kg, String name) {
super(kg); this.name = name;
}
@Override
public String gibLaut () {return Wau! Wau!; }
public String gewinnt (Hund h) {
if (this.name.length() > h.name.length())
return ja;
else return nein;
}
}
public class Canis implements Tier {
private Integer kg;
public Canis(int kg) { this.kg

kg; }
@Override
public String gibLaut () {return Grr;}
public String gewinnt (Canis h) {
if (this.kg > h.kg) return ja;
else
return nein;
}
}
public void eatPrey (Tier t) {
this.kg

kg + t.gibLaut ().length();
}
}
public void eatPrey (Tier t) { kg

kg+1; }
}
public Integer getKg() { return kg; }


a
12 P

Zeichnen Sie zu diesem Code das UML-Klassendiagramm!

Deine Antwort:

Abschnitt MAIN-2eb85ea9-0fdf-4a12-8144-2043089e4d8f

Gemischt
Lösung Aufgabe 3 (Hoare Logik)
12 P

Alle Variablen i,j,x,y,z are declared and have the type int, as well as a with type int[]. Only one answer per subtask.


a
5 P

Decide without proof about the validity of the given Hoare-Triple.
(i) {x>0x >0x>0} x=y+zx=y+zx=y+z; {x=z+yz+y>0x=z+y^ z + y >0x=z+yz+y>0}

gültiges Hoare-Tripel
partiell-gültiges H.-T.
ungültiges H.-T.

Wähle eine Antwort aus

b
5 P

(ii) {x=42y<0z=7x = 42 ^ y < 0 ^ z = 7x=42y<0z=7} z++;y−−;x=yzz++; y--; x = y zz++;y−−;x=yz; {x>69Vz<69x > 69 V z < 69x>69Vz<69}

gültiges Hoare-Tripel
partiell-gültiges H.-T.
ungültiges H.-T.

Wähle eine Antwort aus

c
5 P

(iii) {a=[8,4,2]i=0,1,2j=0,1,2a = [8, 4, 2] ^ i = {0, 1, 2} ^ j = {0,1,2}a=[8,4,2]i=0,1,2j=0,1,2} if(a[i]<a[j]) r=j-i; else r=i-j; {r≤0r≤0r≤0}

gültiges Hoare-Tripel
partiell-gültiges H.-T.
ungültiges H.-T.

Wähle eine Antwort aus

d
5 P

(iv) {x=0,y=2,z=4x = 0, y = 2, z = 4x=0,y=2,z=4} while(x<y) {x++; z++; X--; ;} {z=8z = 8z=8}

gültiges Hoare-Tripel
partiell-gültiges H.-T.
ungültiges H.-T.

Wähle eine Antwort aus

Abschnitt MAIN-025d84d4-06ff-40ec-bdbd-485cb8f1cdf4

Gemischt
Fortsetzung von Aufgabe 2
1 P

b
1 P

Ändern Sie die Grammatik ab, so dass (Ausdruck) beliebige ganze Zahlen als Konstanten ohne führende Nullen erlaubt, d.h. erlaubt sein sollen zum Beispiel die Wörter “1” “2” “3”, “0”, “—” “0” oder “”“5” “6” “7” “8”, aber nicht “0” “0” “1” “2”, oder “1” “2” “_” “2” “3”

Deine Antwort:

iii
1 P

"switch" "C" "x" "=" "x" "+"“3” ")" "{” "case" "3" ":" "z"“=”4“break” ";" "}" ";"

Deine Antwort:
iconlogo
Einloggen