OULUN YLIOPISTO Tietojenkäsittelytieteiden laitos Johdatus ohjelmointiin 81122P (4 ov.) 30.5.2005 Ohjelmointikieli on Java. Tentissä saa olla materiaali mukana. Tenttitulokset julkaistaan aikaisintaan 28.6.2005 1. [ 6 p.] Valitse oikea vaihtoehto, vain yksi per tehtävä (max 1p/kohta). 1.1Seuraavat ovat esimerkkejä Javan valintarakenteista: a) while, do ja for b) if, if-else ja switch c) loop ja while d) while, do, if, switch ja for 1.2Mikä alla esiteltävistä lauseista on Javan syntaksin mukainen: a) if(x > y) then ( System.out.println("x")) b) if(x > y) c) if x > y d) if(x > y) then 1.3Lohkorakenne muodostetaan käyttämällä a) puolipistettä b) aaltosulkeita c) BEGIN ja END lausekkeita d) sisennystä 1.4Mitä tekee ++-operaattori a) Loogisen OR-operaation b) kasvattaa muuttujan arvoa yhdellä c) Loogisen AND-operaation d) && ei ole Javan operaattori ollenkaan 1.5Montako x-merkiä alla oleva ohjelmanpätkä tulostaa int i=0; while(100 > i ) { i++; a) 99 b) 100 c) 101 d) 0 ( Kääntöpuolella lisää tehtäviä!)
1.6Mihin taulukon elementtiin lause arr[9]=10; viittaa, kun taulukko on määritelty: int [] arr = new int [10]; a) taulukon arr yhdeksänteen elementtiin b) taulukon arr kymmenenteen elementtiin c) viittaus kohdistuu taulukon ulkopuolelle d) viittaus kohdistuu taulukon yhdenteentoista elementtiin 2. [ 6 p.] Lisää alla esitettyyn ohjelmarunkoon toiminnot, joiden avulla ohjelma varaa tilaa merkkitaulukolle, jonka koko kysytään käyttäjältä. Ohjelmassa on varmistettava ettei käyttäjä anna taulukon kooksi nollaa tai sitä pienempää arvoa. Tämän jälkeen ohjelma täyttää taulukon lukemalla käyttäjältä merkkejä. Luettu merkki talletetaan taulukkoon jos merkki on joku merkeistä: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z Muut merkit jätetään huomioimatta, taulukko on kuitenkin täytettävä viimeistä sijaa myöten! import java.io.*; class Koe { 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 saa parametrinä merkkitaulukon. Metodi tallettaa taulukon sisällön tekstitiedostoon, joka avataan ja suljetaan tässä metodissa. Ohjelmassa on varmistettava, että tiedoston avaaminen onnistui. 4. [ 6 p.]. Määrittele metodi public static void etsi(char [] merkkitaulukko). Metodi laskee merkkitaulukon merkkien: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z esiintymislukumäärät ja tulostaa lukumäärät kuvaruudulle sopivasti muotoiltuna.
UNIVERSITY OF OULU Department of Information Processin g Science Introduction to programming 81122P (4 ov.) 30.5.2005 Programming language is Java. The result of the exam comes earliest at 28.6.2005. 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 selection statements in Java: a) the while, do, and for statements b) the if, if-else, and switch statements c) the while, do, if, switch and for statements 1.2 Which statement uses correct syntax? a) if( y > x) then (System.out.println("x")); b) if( y > x) c) if y > x d) if (y > x) then 1.3 Block statements are identified by a) semicolons b) braces c) indentation 1.4 What does this operator && perform? a) Logical OR b) Logical AND c) This is not valid operator 1.5 Given the following code fragment: int i=0; while(100 > i ) { i++; How many x's will be displayed a) 99 b) 100 c) 101 d) 0 (turn paper!)
1.6 Given the following code: int [ ]arr = new arr[10]; statement arr [9]=10; refers to a) ninth elemnet of the array arr b) tenth element of the array arr c) it refers outside of the array arr 2. [ 6 p.] Add new code to the program below. Define an array of Strings, so that the user gives the size of that array and program should detect if user gives values that are zero or negative. Then program reads strings that are saved in that array, import java.io.*; class Exam { public static void main ( String [] argv ) throws IOException { BufferedReader stdin = new BufferedReader ( new InputStreamReader ( System.in )); // end of main //end of class Exam 3. [6 p.] Write a method that gets array of Strings as parameter. Method counts the number of vowels (a, e, i,o, u, y )of all strings of the array and then returns this value to the caller of that method. 4. a[ 3 p.] Transform the following while-loop into an equivalent do-while- loop (make sure that it produces the same output). int num = 1; while( num < 20 ){ num++; System.out.println( num ); 4. b[ 3 p.] Transform the while-loop of question 4a into an eqivalent for-loop. (make sure that it produces the same output).