OULUN YLIOPISTO Tietojenkäsittelytieteiden laitos Johdatus ohjelmointiin 811122P (5 op.) 12.12.2005 Ohjelmointikieli on Java. Tentissä saa olla materiaali mukana. Tenttitulokset julkaistaan aikaisintaan 12.1.2006 1. [ 6 p.] Valitse oikea vaihtoehto, vain yksi per tehtävä (max 1p/kohta). 1.1 Seuraavat ovat esimerkkejä Javan toistorakenteista: a) while, do-while ja for b) if, if-else ja switch c) loop ja while d) while, do, if, switch ja for 1.2 Mikä alla esiteltävistä lauseista Javan syntaksin mukainen: a) if(x>y) then ( System.out.println("x")) b) if(x>y) c) if(x>y); System.out.println("x") d) if x>y 1.3 Lohkorakenne muodostetaan käyttämällä a) puolipistettä b) aaltosulkeita c) BEGIN ja END lausekkeita d) sisennystä 1.4 Mitä tekee &&-operaattori a) Loogisen TAI-operaation b) kasvattaa muuttujan arvoa yhdellä c) Loogisen JA-operaation d) && ei ole Javan operaattori ollenkaan 1.5 Montako x-merkiä alla oleva ohjelmanpätkä tulostaa int i=0; while( i++ < 100) { } a) 99 b) 100 c) 10 d) 0 ( Kääntöpuolella lisää tehtäviä!)
1.6 Mihin taulukon elementtiin käsky arr[10] = 10; tallettaa arvon? Taulukko on määritelty: int [] arr = new int[10]; a) taulukon yhdeksänteen elementiin b) taulukon kymmenenteen elementtiin c) viittaus kohdistuu taulukon ulkopuolelle 2. [ 6 p.] Lisää alla olevaan ohjelmarunkoon käskyt, joiden avulla ohjelma tulostaa seuraavanlaisen pyramidin toistorakenteita käyttäen (ns. hardkoodaatusta vastauksesta ei anneta pisteitä). import java.io.*; class Koe { 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 0 1 0 9 8 7 6 7 8 9 0 1 2 3 2 1 0 9 8 7 8 9 0 1 2 3 4 5 4 3 2 1 0 9 8 9 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 9 0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0 public static void main ( String [] argv ) throws IOException { BufferedReader stdin = new BufferedReader ( new InputStreamReader ( System.in )); // lisätään vastauskoodi ja kommentit tähän väliin }// main päättyy }// class päättyy 3. [ 6 p.] Määrittele metodi joka pyytää ja lukee kellonajan muodossa: tunnit:minuutit:sekunnit (esim: 12:22:30). Metodi palauttaa ajan muutettuna sekunneiksi. Ohjelma varmistaa että käyttäjän syöttämä aika on järkevä. (StringTokenizer -luokasta voi olla apua). 4. [6 p.] Määrittele metodi, joka saa parametrinä reaalilukutaulukon. Metodi tallettaa taulukon arvot tekstitiedostoon, joka avataan ja suljetaan tässä metodissa.
UNIVERSITY OF OULU Department of Information Processing Science Introduction to Programming (811122P) (5 op.) 12.12.2005 Programming languae is Java. Literary material is allowed in the exam. The result of the exam comes earliest at 12.1.2006 1. [6 p.] Select one of the possibilities in each question 1.1-1.6 (1p/right answer). 1.1 The following are examples of repetition statements in Java: a) the while, do-while, and for statements b) the if, if-else, and switch statements c) the loop and while statements d) the while, do, if, switch and for statements 1.2 Which statement uses correct Java syntax?: a) if(x>y) then ( System.out.println("x")) b) if(x>y) c) if(x>y); System.out.println("x") d) if x>y 1.3 Block statements are identified by a) semicolons b) braces c) BEGIN and END statements d) indentation 1.4 What does this operator && perform? a) Logical OR b) Decrement by 1 c) Logical AND d) This is not valid Java-operator 1.5 Given the following code, how many x-characters will be displayed? int i=0; while( i++ < 100) { printf("x"); } a) 99 b) 100 c) 10 d) 0 ( Pelase, turn paper!)
1.6 Given the following code: int [] arr = new int 10]; statement arr[10]=10; refers to a) ninth element of the array arr b) tenth element of the array arr c) it refers outside of the array arr 2. [ 6 p.] Fill the program below, so that it prints the pyramid below using repetition statements (do not use "hardcoding"). import java.io.*; class Exam { 1 2 3 2 3 4 5 4 3 4 5 6 7 6 5 4 5 6 7 8 9 8 7 6 5 6 7 8 9 0 1 0 9 8 7 6 7 8 9 0 1 2 3 2 1 0 9 8 7 8 9 0 1 2 3 4 5 4 3 2 1 0 9 8 9 0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 9 0 1 2 3 4 5 6 7 8 9 8 7 6 5 4 3 2 1 0 public static void main ( String [] argv ) throws IOException { BufferedReader stdin = new BufferedReader ( new InputStreamReader ( System.in )); // your answer in here }// main ends }// class ends here 3. [ 6 p.] Write method that reads time from the user. Format of the input is: hours:minutes:seconds (Eample: 12:22:30). Method returns time in seconds. Method should check that given time is realistic. (Class StringTokenizer may be usefull). 4. [6 p.] Write method that gets array of reals as parameter. Method writes values of the array to the textfile that is opened and closed in this method.