예상문제은행 Part 1
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int x = 15, y = 7, z = 3, w = 2;
x /= y++ - z * w;
System.out.printf("%d", x);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
static final int ODD = 1;
static final int EVEN = 0;
public static void main(String[] args) {
int value = 42;
if (value % 2 == EVEN)
System.out.printf("APPLE");
else if (value % 2 == ODD)
System.out.printf("BANANA");
else
System.out.printf("CHERRY");
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int arr[] = {7, 6, 5, 4, 3};
int k = 4, total = 0;
do {
arr[k] = arr[k] % 4;
total = total + arr[k];
k--;
} while (k > 0);
System.out.printf("%d", total);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
static void countdown(int n) {
if (n <= 0)
return;
System.out.printf("%d ", n);
countdown(n - 1);
}
public static void main(String[] args) {
countdown(4);
}
}
다음은 피보나치 수를 구하는 Java 프로그램이다. 괄호 ①, ②에 들어갈 알맞은 코드를 쉼표로 구분하여 작성하시오.
public class Test {
static int Fibo(int n) {
if (n == 0)
return 0;
else if (n == 1)
return ( ① );
else
return Fibo(( ② )) + Fibo(n - 1);
}
public static void main(String[] args) {
for (int i = 0; i < 8; i++)
System.out.printf("%d ", Fibo(i));
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
for (int k = 7; k > 0; k--) {
switch (k % 2) {
case 1:
System.out.printf("%d", k);
break;
default:
System.out.printf("#");
break;
}
}
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int x, y = 15;
for (x = 0; x < 4; ++x, y -= x);
System.out.printf("%d, %d", x, y);
}
}
다음 Python 프로그램의 실행 결과를 작성하시오.
data = [0, 0, 0, 0, 5]
for m in range(4, -1, -1):
for n in range(4, m, -1):
data[m] += data[n]
for m in range(5):
print(data[m], end=" ")
다음 Python 프로그램의 실행 결과를 작성하시오.
def remove_char(text):
return text.replace('-', '')
s = "A-B-C-D-E-F"
s = remove_char(s)
print(s)
다음은 50 이하의 소수 개수를 구하는 Python 프로그램이다. 괄호에 들어갈 알맞은 조건식을 작성하시오.
def is_prime(num):
for k in range(2, num):
if ( ):
return 0
return 1
limit = 50
count = 0
for k in range(2, limit + 1):
count += is_prime(k)
print(f"{limit} 이하의 소수는 {count}개입니다.")
예상문제은행 Part 2
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
String msg = "CODE" + 20 + 25;
System.out.println(msg);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
char val = 0x05;
System.out.printf("%04x", val << 3);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int m = 13;
int n = 7;
int result = m ^ n;
System.out.println(result);
}
}
다음은 홀수의 합을 구하는 Java 프로그램이다. 괄호에 들어갈 알맞은 코드를 작성하시오.
public class Test {
public static void main(String[] args) {
int total = 0;
for (int k = 1; k <= 10; k++) {
if (k % 2 == 1)
( );
}
System.out.println("홀수의 합 = " + total);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int result = multiply(5, 3);
System.out.println(result);
}
public static int multiply(int x, int y) {
int product;
return product = x * y;
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int p, q, r, answer;
p = 15;
q = 25;
r = 35;
answer = p < q ? q++ : --r;
System.out.printf("%d %d %d\n", answer, q, r);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
sb.append("JAVA");
sb.insert(2, "TEST");
System.out.print(sb.toString());
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Main {
public static void main(String[] args) {
int[] nums = {2, 7, 4, 9, 3, 6};
Filter f = new Filter();
f.process(nums, 3);
}
}
class Filter {
void process(int[] arr, int mod) {
int k;
if (mod > 1) {
for (k = 0; k < arr.length; k++) {
if ((k + 1) % mod == 0)
System.out.printf("%d", arr[k]);
}
} else {
for (k = 0; k < arr.length; k++) {
System.out.printf("%d", arr[k]);
}
}
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int val;
val = 25;
modify(val);
System.out.printf("%d", val);
}
static void modify(int val) {
val = val + 5;
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
String text = "#hello#";
int len = text.length();
char[] chars = new char[len];
len--;
for (int i = len; i >= 0; i--)
chars[len - i] = text.charAt(i);
for (char c : chars)
System.out.printf("%c", c);
}
}
예상문제은행 Part 3
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int k, total = 0;
for (k = 1; k <= 8; ++k, total += k);
System.out.printf("%d, %d\n", k, total);
}
}
다음은 입력받은 숫자의 홀짝 여부를 판단하는 Java 프로그램이다. 괄호에 들어갈 알맞은 조건식을 작성하시오.
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int number;
Scanner sc = new Scanner(System.in);
number = sc.nextInt();
if ( ( ) == 0 )
System.out.printf("%d는 짝수\n", number);
else
System.out.printf("%d는 홀수\n", number);
sc.close();
}
}
다음 Java 프로그램의 실행 결과를 작성하시오. (줄바꿈은 /로 구분)
public class Test {
public static void main(String[] args) {
int n = 0, acc = 0;
for (n = 1; n <= 4; ++n, acc += n)
System.out.printf("n=%d acc=%d\n", n, acc);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
int m, n, result;
m = 15;
n = 25;
result = calc(m, n);
System.out.printf("m=%d, n=%d, result=%d\n", m, n, result);
}
static int calc(int x, int y) {
int z;
if (x == y) z = x + y;
else z = x - y;
return(z);
}
}
다음 Java 프로그램의 괄호에 들어갈 알맞은 예약어를 <보기>에서 찾아 기호로 작성하시오.
interface Calculator {
public void compute(int n);
}
class Doubler ( ) Calculator {
public void compute(int n) {
System.out.print(n * 2);
}
}
public class Test {
public static void main(String[] args) {
Calculator c = new Doubler();
c.compute(25);
}
}
// 실행결과: 50
<보기>
ㄱ new
ㄴ abstract
ㄷ super
ㄹ extends
ㅁ implements
다음 Python 프로그램의 괄호에 들어갈 예약어를 작성하시오.
( ) Product:
name = '노트북'
code = 'A1001'
price = 1500000
available = True
item = Product()
print(item.name)
print(item.code)
print(item.price)
print(item.available)
다음 Python 프로그램의 실행 결과를 작성하시오.
n = total = 0
while n < 12:
n += 1
if n % 3 != 0:
continue
total += n
print(total)
다음 Python 프로그램의 실행 결과를 작성하시오.
x, y = 5, 6
z = x | y
print(z)
다음 Python 프로그램의 실행 결과를 작성하시오.
total = 0
for k in range(1, 8):
total += k
print(k, total)
다음 Python 프로그램의 실행 결과를 작성하시오. (줄바꿈은 /로 구분)
s = "Hello World"
print("%-12.5s" % s)
print("%12.5s" % s)
다음 Python 프로그램의 실행 결과를 작성하시오.
val1 = 15 + 15 % 6 - 15 % 7
val2 = 15 * 15 % 6 - 15 % 7 + 3
print("%d, %d" % (val1, val2))
예상문제은행 Part 4
다음 Python 프로그램의 실행 결과를 작성하시오.
x, z = 16, -2
y = x << 3
x >>= 2
z = z << 3
print(x, y, z)
다음은 1부터 50까지의 합을 구하는 Python 프로그램이다. 괄호에 들어갈 알맞은 조건식을 작성하시오.
n, total = 0, 0
while ( ):
n += 1
total += n
if n >= 50:
break
print(total)
다음 Python 프로그램의 실행 결과를 작성하시오.
def swap():
global a, b
temp = a
a = b
b = temp
a, b = 30, 50
swap()
print(f"a={a}, b={b}")
다음 Python 프로그램의 실행 결과를 작성하시오.
word = 'Python'
length = len(word)
chars = list()
for idx in range(length):
chars.append(word[idx])
for idx in range(length-1, -1, -1):
print(chars[idx], end='')
다음 Python 프로그램의 실행 결과를 작성하시오.
n, acc = 1, 0
while n <= 8:
acc += n
n += 3
print(f"n={n}, acc={acc}")
다음 Python 프로그램의 실행 결과를 작성하시오.
matrix = [[1, 0, 1, 0, 1],
[0, 1, 0, 1]]
total, count = 0, 0
for row in matrix:
for val in row:
total += val
count = count + len(row)
print(count, total)
다음은 피보나치 수열의 합을 구하는 Python 프로그램이다. 괄호 ①, ②에 들어갈 알맞은 답을 쉼표로 구분하여 작성하시오.
first, second = 1, 1
total = first + second
count = int(input())
for k in range(3, count + 1):
third = first + second
total = ( ① )
first = second
( ② )
print(total)
다음 Python 프로그램의 실행 결과를 작성하시오.
def display(val1, val2 = 5):
print('x =', val1, ', y =', val2)
display(15)
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void main(String[] args) {
String text = "AAXAAYAAZAA";
String[] parts = text.split("A");
System.out.print(parts[2]);
}
}
예상문제은행 Part 5
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
static class Item {
int data;
Item next;
Item(int d) { this.data = d; }
}
static void process(Item item) {
while (item != null && item.next != null) {
int temp = item.data;
item.data = item.next.data;
item.next.data = temp;
item = item.next.next;
}
}
public static void main(String[] args) {
Item i1 = new Item(5);
Item i2 = new Item(10);
Item i3 = new Item(15);
i1.next = i3;
i3.next = i2;
process(i1);
process(i1);
Item cur = i1;
while (cur != null) {
System.out.printf("%d", cur.data);
cur = cur.next;
}
}
}
다음 Java 프로그램의 실행 결과를 작성하시오. (줄바꿈은 /로 구분)
class Counter {
public int x = 30;
static int y = 0;
}
public class Test {
public static void main(String[] args) {
int x = 15;
Counter.y = x;
Counter c = new Counter();
System.out.println(Counter.y++);
System.out.println(c.y);
System.out.println(x);
System.out.print(c.x);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오. (줄바꿈은 /로 구분)
public class Test {
public static void main(String[] args) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = new String("Hello");
System.out.println(s1 == s2);
System.out.println(s1 == s3);
System.out.println(s1.equals(s3));
System.out.println(s2.equals(s3));
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
class DataBox {
int p;
int q;
int r;
}
public class Test {
public static void main(String[] args) {
DataBox box = new DataBox();
box.p = 20;
box.q = 30;
compute(box);
System.out.printf("p=%d, q=%d, r=%d\n",
box.p, box.q, box.r);
}
static void compute(DataBox box) {
box.p += 10;
box.q -= 10;
if (box.p <= box.q)
box.r = box.p + box.q;
else
box.r = box.p - box.q;
}
}
다음 Java 프로그램의 실행 결과를 작성하시오. (줄바꿈은 /로 구분)
public class Test {
static int[] data = new int[4];
static int ptr = -1;
public static void main(String[] args) {
add(50);
add(60);
add(70);
remove();
add(80);
add(90);
remove();
}
static void add(int val) {
ptr++;
if (ptr >= 4)
System.out.printf("overflow");
else
data[ptr] = val;
}
static void remove() {
if (ptr < 0)
System.out.printf("underflow");
else
System.out.printf("%d삭제\n", data[ptr--]);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static void compare(int[] m, int[] n) {
if (m == n)
System.out.print("Y");
else
System.out.print("X");
}
public static void main(String[] args) {
int p[] = new int[] {5, 10, 15};
int q[] = new int[] {5, 10, 15};
int r[] = new int[] {5, 10};
compare(p, q);
compare(q, r);
compare(p, r);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
public static String unique(String s, int idx, boolean[] check) {
if (idx < 0) return "";
char ch = s.charAt(idx);
String result = unique(s, idx - 1, check);
if (!check[ch]) {
check[ch] = true;
return ch + result;
}
return result;
}
public static void main(String[] args) {
String text = "xyzxyxz";
int len = text.length();
boolean[] check = new boolean[256];
System.out.print(unique(text, len - 1, check));
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
interface Filter {
int calculate(int[] arr, boolean positive);
}
public class Test {
public static void main(String[] args) {
int nums[] = {-3, -2, -1, 0, 1, 2, 3};
SignFilter sf = new SignFilter();
System.out.print(sf.calculate(nums, true) + ", " + sf.calculate(nums, false));
}
}
class SignFilter implements Filter {
public int calculate(int[] arr, boolean positive) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
if ((positive && arr[i] > 0) || (!positive && arr[i] < 0)) {
sum += arr[i];
}
}
return sum;
}
}
다음 Python 프로그램의 실행 결과를 작성하시오.
def find(text, pattern):
count = 0
for idx in range(len(text)):
part = text[idx:idx+len(pattern)]
if part == pattern:
count += 1
return count
text = "abcabcabcabc"
p1 = "bc"
p2 = "ca"
print(f"bc{find(text, p1)} ca{find(text, p2)}")
다음 Java 프로그램의 실행 결과를 작성하시오.
class Output {
void show(Integer x) {
System.out.print("X" + x);
}
void show(Object x) {
System.out.print("Y" + x);
}
void show(Number x) {
System.out.print("Z" + x);
}
}
public class Test {
public static void main(String[] args) {
new Container<>(5).display();
}
public static class Container<T> {
T item;
public Container(T t) {
item = t;
}
public void display() {
new Output().show(item);
}
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
public class Test {
static String[] arr = new String[3];
static void check(String[] arr, int size) {
for (int k = 1; k < size; k++) {
if (arr[k - 1].equals(arr[k])) {
System.out.print("Y");
} else {
System.out.print("X");
}
}
for (String s : arr) {
System.out.print(s);
}
}
public static void main(String[] args) {
arr[0] = "B";
arr[1] = "B";
arr[2] = new String("B");
check(arr, 3);
}
}
다음 Java 프로그램의 실행 결과를 작성하시오.
class Database {
private static Database _instance = null;
private int queries = 0;
public static Database getInstance() {
if (_instance == null) {
_instance = new Database();
return _instance;
}
return _instance;
}
public void query() {
queries++;
}
public int getQueries() {
return queries;
}
}
public class Test {
public static void main(String[] args) {
Database db1 = Database.getInstance();
db1.query();
db1.query();
Database db2 = Database.getInstance();
db2.query();
Database db3 = Database.getInstance();
db3.query();
db3.query();
System.out.print(db1.getQueries());
}
}
핵심 요약
Java 핵심 포인트
- 연산자 우선순위: 단항 > 산술 > 시프트 > 관계 > 비트 > 논리 > 삼항 > 대입
- 증감연산자:
++x(전위),x++(후위) 차이 주의 - 문자열 비교:
==는 주소 비교,equals()는 내용 비교 - static 변수: 클래스 변수로 모든 인스턴스가 공유
- 인터페이스:
implements키워드로 구현
Python 핵심 포인트
- 비트연산:
&(AND),|(OR),^(XOR),<<(좌시프트),>>(우시프트) - 슬라이싱:
[시작:끝:증가], 끝 인덱스 미포함 - global 키워드: 함수 내에서 전역변수 수정 시 필요
- 기본 매개변수: 함수 정의 시 기본값 지정 가능
자료구조
- 스택: LIFO(후입선출), push/pop 연산
- 연결리스트: 노드와 포인터로 구성
- 싱글톤: 인스턴스를 하나만 생성하는 디자인 패턴