2009Spring_mid - 1 public class Q1_1 public static void...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: - 1 - public class Q1_1 { public static void main(String[] args) { int a = 2; String s = ""; switch(a){ case 0: s += "Turn on the light. "; break; case 1: s += "Turn off the light. "; break; case 2: s += "Turn on the TV. "; default : s += "Do nothing. "; } System.out.println(s); } } 1 (30 points) 2 (20 points) 3 (30 points) 4 (20 points) TOTAL (100 points) CS101 Introduction to Programming 2009 Spring Midterm Examination SECTION STUDENT ID NAME ※ Fill out the above information CORRECTLY or you will lose 1 point for each mistake. 1. Answer each question according to the instruction. 1-1. What is the result of the following program? ( ② ) (3 points) ① Turn on the TV. ② Turn on the TV. Do nothing. ③ Do nothing. ④ Turn on the light. ⑤ Turn on the light. Turn off the light. Turn on the TV. Do nothing.- 2 - 1-2. Choose an incorrect description in the following program. ( ⑤ ) (3 points) public class Sample { public static void main(String[] args) { System.out.println("Hello World!"); } } ① This program should be in "Sample.java" file. ② System.out is an object for sending output to the screen. ③ println is a method to print a string in parentheses to the screen. ④ Execution always starts at the main method. ⑤ When this program is compiled, the byte-code version of the program will be stored in the "Main.class" file. 1-3. What is the result of the following program? ( ① ) (3 points) public class Q1_3 { public static void main(String[] args) { int height = 3; int num = 1; for(int i = 0; i < height; i++) { for(int j = 0; j < i; j++) { System.out.print(" ‐ "); } for(int j = 0; j < height ‐ i; j++) { System.out.print(num+" ‐ "); num++; } System.out.println(); } } } ① 1-2-3--4-5---6- ② 1-2-3- 4-5- 6- ③ 1- 2-3- 4-5-6- ④--1- -2-3- 4-5-6- ⑤ 1-2-3---4-5-----6-- 3 - import java.util.*; public class Factorial { public static void main(String[] args){ Scanner s = (1) Scanner(System.in); int n, result; n = (2) ; // read a number from keyboard (3) ; // initialize the variable result if(n < 0) System.out.println("The input number should be a positive integer."); else { for(int i = 1; i <= n; i++) { (4) ; // calculate factorial repeatedly System.out.print("The factorial of " + i + " is "); System.out.println(result); } } } } 4 The factorial of 1 is 1 The factorial of 2 is 2 The factorial of 3 is 6 The factorial of 4 is 24 ① result = 1 ② result = 0 ③ result *= i ④ result += i ⑤ s.next() ⑥ class ⑦ new ⑧ s.nextInt() ⑨ s.nextDouble() (1) ⑦ new (3) ① result = 1 (2) ⑧ s.nextInt() (4) ③ result *= i 1-4. The following program is to compute factorial. Choose suitable keywords from ( ① ~ ⑨ ) for blank boxes (1)~(4) (6 points - (1) 1pts, (2) 1pts, (3) 2pts, (4) 2pts) <Execution results> : If you type ' 4 ' in the keyboard, the execution result is as follows- 4 - public class arrayTest { public static void main(String[] args) { int[] a = new int[5]; for(int i = 0; i < a.length; i++) a[i] = i; System.out.println(a[0] + a[1] + a[3]);System....
View Full Document

{[ snackBarMessage ]}

Page1 / 15

2009Spring_mid - 1 public class Q1_1 public static void...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online