Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
46 views4 pages

24 1 MidTerm OOP

Uploaded by

황보현
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views4 pages

24 1 MidTerm OOP

Uploaded by

황보현
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Object-Oriented Programming Mid-term Exam Student ID:

Name:

1. For each of the following codes (a) ~ (e), write the output (15)._______
// (a)
System.out.println(12345 + 5432.1);
System.out.println(12345 + (int)5432.1);

// (b)
System.out.println("H" + "A");
System.out.println('H' - 'A');

// (c)
String pig = "length: 10";
String dog = "length: " + pig.length();
System.out.println(dog);
System.out.println("Animals are equal: " + pig == dog);

// (d)
int minutes = 0;
for (int ms = 0; ms < 60*60*1000; ms++)
if ((ms % 60) * 1000 == 0) minutes++;
System.out.println(minutes);

// (e)
int END = Integer.MAX_VALUE;
int START = END - 10;
int count = 0;
for (int i = START; i < END; i++)
count++;
System.out.println(count);

2. For each of the following programs (a) ~ (e), write the output. If there is an error, give your correction
first and then the output (15).
(a) public class MyClass {
public static void main(String[] args) {
for (int i = 0; 1; i++) {
System.out.println("Hello");
break;
}
}

(b) public class MyClass {


public static void main(String args[]) {
System.out.println(fun());
}
int fun() {
return 20;
}
J________________________________________________

(c) public class MyClass {


private int x = 2;
MyClass(int i) { x = ++i; }
public static void main(String[] args) {
MyClass t = new MyClass(5);
System.out.println("x = " + t.x);
}
}________________________________________________

2024/4/22 1
Object-Oriented Programming Mid-term Exam Student ID:
Name:

(d) public class MyClass {


public static void main(String args[]) {
System.out.println(fun());
}
static int fun() {
static int x = 0;
return ++x;
}
J___________________________________________________________

(e) // MyClassl.java
public class MyClassl {
MyClassl(int x) {
System.out.println("Constructor called " + x);
}
}

// MyClass2.java
public class MyClass2 {
MyClassl tl = new MyClassl(10);
MyClass2(int i) { tl = new MyClassl(i); }
public static void main(String[] args) {
MyClass2 t2 = new MyClass2(5);
}

3. Write the output of the following code (15).____________________________


public class EnumClass {
public enum State {
CoLorado("CO"), Hawoii("HI"), Minnesota("MN"),
Oregon("OR"), Washington("Wh");

String acronym;
State(String acronym) {
this.acronym = acronym;
}
public String getAcronym() {
return acronym;
}
}

public static void main(String[] args) {


State state = State.Minnesota;
System.out.println(state.ordinal());
System.out.println(state .equals(State.CoLorado));
System.out.println(state.compareTo(State.CoLorado));
System.out.println(State.vaLueOf( "Colorado"));
System.out.println(State.vaLueOf(state.toString()));
System.out.println(state.getAcronym());
System.out.println(state.toString());
}
}

4. Fill in the blanks (a) ~ (h) in the following code. The code receives a phone number (String) from the
keyboard and prints the count for each digit 0 ~ 9. For example, it prints frequency[O] = 2,
frequency[l] = 2, ..., frequency[9] = 0 for a given input "010-1234-3456" (15).

2024/4/22 2
Object-Oriented Programming Mid-term Exam Student lD:
Name:

public static void main(String[] args) {


int[] frequency = | (a) |;
for (int i = 0; i < frequency.| (b) |; i ++) frequency[i] = 0;

Scanner kb = new Scanner(| (c) |);


System.out.println("Enter a phone number");
String p = kb.| (d) |;

for (int i = 0; i < p.p(e) |; i ++) {


char ch = p.| (f) |(i);
if (!Character.| (g)^|(ch)) continue;
int index = Character.| (h) |(ch);
frequency[index] ++;
}

for (int i = 0; i < frequency.| (b) |; i ++) {


System.out.println("frequency[" + i + ''] = " + frequency[i]);
}
kb.close();

5. The following is a static method that takes an integer h (0 < n < 100,000) and returns a String
containing the Korean reading of n. For example, if n equals 12345, the method would return
". Identify all the errors in the code; provide the line number and a correction
for eac a error (20),______________________________________________________________________
1 public static String reading(int n) {
2 String digit[] = {"^"/'^V'O|"/'At"/'ApV^"/'^","^"/'^V'T1"};
3 String unit[] = {"oi","^","^","^","^"};
4 if ((0 < n) && (n < 100000)) return s;
5
6 String s = null;
7 int div = 10000, d = 0, u = 0;
8 while (n > 0) {
9 d = (int)(n / div);
10 n %= div;
11 if (d >= 0) s += digit[d] + unit[u];
12 div *= 10;
13 }
14 }____________________________________________________________________________________

6. The following program sorts an array of int-type elements in ascending order. Locate all errors;
arovide the line number and a correction for each of them. (15).
1 public class BubbleSort {
2 public void bubbleSort(int arr) {
3 for (int i = 0; i < arr.size; i ++) {
4 for (int j = i + 1; j < arr.size; j ++) {
5 if (arr[i] < arr[j]) {
6 int temp = arr[i];
7 arr[j] = arr[i];
8 arp[J] = temp;
9 }
10 }
11 }
12 }
13 public static void main(String[] args) {
14 _________ int arr = {3, 6, 8, 2, 5, 4, 7, 1};________________________________

2024/4/22 3
Object-Oriented Programming Mid-term Exam Student ID:
Name:

15 bubbLeSort(arr);
16 }
17 }

7. Write a static method isPalindrome(String word) that checks if the given word is a palindrome. A
palindrome is a word that reads the same backward or forward. Character cases should be ignored.
For example, isPalindrome("Deleveled") should return true (15).
public static boolean isPalindrome(String word) {
// Place your code here.
}

2024/4/22 4

You might also like