* 위 게시글을 바탕으로 코드 정리를 하였습니다.
Java
1. 배열 결과 입력하시오.
public class Test {
static int nSize = 4;
public static void main(String[] args){
int[] arr = new int[nSize];
makeArray(arr);
for(int i = 0; i<nSize; i++){
System.out.print(arr[i] + " ");
}
}
public static void makeArray(int[] arr) {
for(int i = 0; i<nSize; i++){
arr[i] = i;
}
}
}
답안
0 1 2 3
2. 자바 객체 생성( ) 채우기 (2020년 2회)
class Parent {
void show(){
System.out.println("Parent");
}
}
class Child extends Parent {
void show(){
System.out.println("Child");
}
}
public class Test {
public static void main(String[] args){
Parent pa = ( ) Child();
pa.show();
}
}
출력결과
Child
답안
new
3. 자바 상속, super 키워드 이용 결과값을 묻는 문제 (2020년 2회)
class A {
int a;
A(int a) {
this.a = a;
}
void display(){
System.out.println("a="+a);
}
}
class B extends A{
B(int a) {
super(a);
super.display();
}
}
public class Test {
public static void main(String[] args){
B obj = new B(10);
}
}
동작설명
- main 메서드에서 new B(10)을 호출하여 B 클래스의 객체를 생성한다.
- B 클래스의 생성자가 호출된다.
- super(a)를 통해 부모 클래스 A의 생성자를 호출한다. 이때 a는 10으로 초기화된다.
- A 클래스의 생성자 A(int a)가 실행되어 this.a = a가 수행된다.
- 부모 클래스의 display() 메서드가 호출된다 (super.display()).
- display() 메서드는 System.out.println("a=" + a)를 출력.
- 이때 a는 10이므로, a=10이 출력.
답안
a=10
4. 다음의 출력 결과를 쓰시오. (2020년 3회)
public class Test {
public static void main(String[] args){
int i = 0;
int sum = 0;
while(i<10){
i++;
if(i%2==1)
continue;
sum += i;
}
System.out.println(sum);
}
}
답안
30
5. 다음의 출력 결과를 쓰시오. (2020년 3회)
abstract class Vehicle{
String name;
abstract public String getName(String val);
public String getName(){
return "Vehicle name : " + name;
}
}
class Car extends Vehicle {
public Car(String val){
name = super.name = val;
}
public String getName(String val){
return "Car name : " + val;
}
public String getName(byte val){
return "Car name : " + val;
}
}
public class Test {
public static void main(String[] args){
Vehicle obj = new Car("Spark");
System.out.println(obj.getName());
}
}
동작 설명
- main 메서드에서 new Car("Spark")를 호출하여 Car 객체를 생성한다.
- Car 생성자가 호출되어 name 필드가 "Spark"로 초기화된다.
- obj.getName()을 호출한다.
- obj는 Vehicle 타입이지만 실제로는 Car 객체를 참조하고 있다.
- Vehicle 클래스의 getName() 메서드가 호출된다.
- name 필드는 "Spark"로 설정되어 있으므로 Vehicle name : Spark를 반환한다.
답안
Vehicle name : Spark
6. 다음 빈 칸에 알맞은 답을 쓰시오. (10을 2진수로 변환) (2020년 4, 5회)
public class Test {
public static void main(String[] args){
int a[] = new int[8];
int i = 0, n = 10;
while( ( ① ) ){
a[i++] = ( ② );
n /= 2;
}
for (i = 7; i >= 0; i--)
System.out.printf("%d", a[i]);
}
}
출력 결과 00001010 |
동작 설명
- while 루프를 통해 10을 이진수로 변환:
- 첫 번째 반복: 10 % 2 = 0, a[0] = 0, n = 5
- 두 번째 반복: 5 % 2 = 1, a[1] = 1, n = 2
- 세 번째 반복: 2 % 2 = 0, a[2] = 0, n = 1
- 네 번째 반복: 1 % 2 = 1, a[3] = 1, n = 0
- 배열 a의 내용은 [0, 1, 0, 1, 0, 0, 0, 0]이다.
- for 루프를 통해 배열 a의 내용을 역순으로 출력한다.
답안
① n > 0 ② n %2
7. 3행 5열 배열 출력 시 배열 크기를 지정하는 문제 (2020년 4, 5회)
public class Test {
public static void main(String[] args){
int [][] array = new int[①][②];
int n = 1;
for (int i = 0; i < 3; i++){
for(int j = 0; j < 5; j++){
array[i][j] = j * 3 + (i+1);
System.out.print(array[i][j]+"");
}
System.out.println();
}
}
}
동작 설명
- 2차원 배열 array를 선언하고 각 요소를 j * 3 + (i+1)로 초기화.
- 배열의 각 요소를 출력하여 3x5 배열의 값을 출력.
- 최종 출력은 위에 설명한 대로 각 행별로 숫자가 증가하는 패턴
답안
① 3 ② 5
8. 실행 결과를 쓰시오. (2020년 4, 5회)
class Parent{
int compute(int num){
if(num <= 1)
return num;
return compute(num-1) + compute(num-2);
}
}
class Child extends Parent {
int compute(int num){
if(num <= 1)
return num;
return compute(num-1) + compute(num-3);
}
}
public class Test {
public static void main(String[] args){
Parent obj = new Child();
System.out.print(obj.compute(4));
}
}
동작 설명
- Child 클래스의 compute 메서드는 compute(num-1) + compute(num-3)을 계산한다.
- compute(4) = compute(3) + compute(1)
compute(3) = compute(2) + compute(0)
compute(2) = compute(1) + compute(-1)
compute(1) = 1
compute(-1) = -1
compute(0) = 0
compute(2) = 1 + (-1) = 0
compute(3) = 0 + 0 = 0
compute(4) = 0 + 1 = 1
- compute(4) = compute(3) + compute(1)
- Parent 타입의 Child 객체가 생성되면, Child 클래스의 compute 메서드가 호출된다.
- obj.compute(4) 호출 시 재귀적으로 값을 계산하여 최종 결과로 1을 출력한다.
답안
1
9. 출력결과 (부분 점수 없음) (2021년 1회)
public class Test {
public static void main(String[] args){
int a[][] = {{45, 50, 75}, {89}};
System.out.println(a[0].length);
System.out.println(a[1].length);
System.out.println(a[0][0]);
System.out.println(a[0][1]);
System.out.println(a[1][0]);
}
}
답안
3
1
45
50
89
10. 출력결과 (부분 점수 없음) (2021년 1회)
public class Test {
public static void main(String[] args){
int i, j;
for(i = 0, j = 0; j <= 5; j++){
i += j;
System.out.print(j);
if(j == 5){
System.out.print("=");
System.out.print(j);
} else {
System.out.print("+");
}
}
}
}
답안
0+1+2+3+4+5=5
11. 이것은 클래스 내에서 객체 생성 없이 사용할 수 있는 메소드이다. 다음의 출력 결과를 보고 괄호 안에 알맞은 답을 작성하시오. (2021년 2회)
public class Test {
public static void main(String[] args){
System.out.print(check(1));
}
( ) String check (int num){
return (num >= 0) ? "positive" : "negative";
}
}
동작 설명
- main 메소드:
- 프로그램이 실행될 때 가장 먼저 호출되는 메소드이다.
- System.out.print(check(1));을 호출하여 check 메소드를 실행한다.
- check 메소드:
- 정적 메소드입니다(static).
- 입력된 정수 num가 0 이상인지 확인한다.
- 삼항 연산자 (num >= 0) ? "positive" : "negative"를 사용하여 num가 0 이상이면 "positive"를, 그렇지 않으면 "negative"를 반환.
답안
static
12. Java 상속, 오버라이딩 (2021년 2회)
public class ovr1 {
public static void main(String[] args){
ovr1 a1 = new ovr1();
ovr2 a2 = new ovr2();
System.out.println(a1.sun(3,2) + a2.sun(3,2));
}
int sun(int x, int y){
return x + y;
}
}
class ovr2 extends ovr1{
int sun(int x, int y){
return x-y+super.sun(x, y);
}
}
동작 설명
- a1.sun(3, 2) 호출:
- ovr1 클래스의 sun(3, 2) 메소드가 호출되어 3 + 2 = 5를 반환한다.
- a2.sun(3, 2) 호출:
- ovr2 클래스의 sun(3, 2) 메소드가 호출된다.
- ovr2 클래스에서 x - y + super.sun(x, y)를 계산하여 반환한다.
- x - y는 3 - 2 = 1
- super.sun(x, y)는 ovr1 클래스의 sun(3, 2)를 호출하여 3 + 2 = 5를 반환한다.
- 따라서 1 + 5 = 6을 반환한다.
- System.out.println(a1.sun(3,2) + a2.sun(3,2)); 출력:
- a1.sun(3, 2)은 5를 반환하고, a2.sun(3, 2)는 6을 반환한다.
- 따라서 총 합은 5 + 6 = 11이 출력된다.
답안
11
C언어
1. 버블 정렬 오름차순 문제(2020년 1회)
#include <stdio.h>
#define SIZE 5
int main(void) {
int arr[SIZE] = {75, 100, 95, 50, 85};
int i, j, temp;
for(i = 1; i < SIZE; i++) {
for(j = 0; j < SIZE-i; j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
for(i = 0; i < SIZE; i++){
printf("%d ", arr[i]);
}
}
동작 설명
- 초기 배열 상태: {75, 100, 95, 50, 85}
- 첫 번째 패스 (i = 1):
- 100과 95를 교환하여 {75, 95, 100, 50, 85}
- 100과 50을 교환하여 {75, 95, 50, 100, 85}
- 100과 85를 교환하여 {75, 95, 50, 85, 100}
- 두 번째 패스 (i = 2):
- 95과 50을 교환하여 {75, 50, 95, 85, 100}
- 95과 85를 교환하여 {75, 50, 85, 95, 100}
- 세 번째 패스 (i = 3):
- 75과 50을 교환하여 {50, 75, 85, 95, 100}
- 네 번째 패스 (i = 4):
- 정렬 완료 {50, 75, 85, 95, 100}
답안
50 75 85 95 100
2. switch~case 결과 입력 (2020년 1회)
#include <stdio.h>
int main(void) {
int c = 1;
switch (3) {
case 1:
c += 3;
case 2:
c++;
case 3:
c = 0;
case 4:
c += 3;
case 5:
c -= 10;
default:
c--;
}
printf("%d", c);
}
동작 설명
- switch 문의 조건은 3이다.
- switch 문은 주어진 값 3에 해당하는 case부터 실행을 시작하고, break 문이 없으면 그 아래의 모든 case 문을 연속으로 실행한다.
답안
-8
3. 다음의 출력 결과를 쓰시오. (2020년 3회)
#include <stdio.h>
int main(void) {
int c = 0;
int i = 0;
while(i < 10) {
i++;
c *= i;
}
printf("%d", c);
}
동작 설명 : c의 초기값이 0이라, c가 증가하지 않는 이상 어떤 값을 곱해도 그냥 다 0이다.
답안
0
4. 다음의 출력 결과를 쓰시오. (2020년 3회)
#include <stdio.h>
int r1(void) {
return 4;
}
int r10(void) {
return (30 +r1()); // 34
}
int r100(void) {
return (200 + r10()); //234
}
int main(void) {
printf("%d\n", r100());
return 0;
}
답안
234
5. 실행 결과를 쓰시오. (2020년 4, 5회)
#include <stdio.h>
int main(void) {
char* p = "KOREA";
printf("%s\n", p);
printf("%s\n", p+3);
printf("%c\n", *p);
printf("%c\n", *(p+3));
printf("%c\n", *p+2);
}
동작 설명
- p 는 문자열 "KOREA"를 가리키는 포인터이다.
- 포인터 p가 가리키는 문자열 전체
- 포인터 p에서 3만큼 이동한 위치부터 시작하는 문자열 출력
- 포인터 p 가 가리키는 첫 번째 문자 출력
- 포인터 p에서 3만큼 이동한 위치의 문자 출력
- 포인터 p가 가리키는 첫 번째 문자에 2를 더한 값
답안
KOREA
EA
K
E
M
6. 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오. (2021년 1회)
#include <stdio.h>
int main(void) {
struct insa {
char name[10];
int age;
}
a[] = {"Kim", 28, "Lee", 38, "Park", 41, "Choi", 30};
struct insa* p;
p = a;
p++;
printf("%s\n", p->name);
printf("%d\n", p->age);
}
동작 설명
- 구조체 정의 및 초기화: struct insa를 정의하고, a 배열을 초기화하여 각 요소에 이름과 나이를 저장한다.
- 포인터 선언 및 초기화: p 포인터를 a 배열의 첫 번째 요소를 가리키도록 초기화한 후, p++을 통해 두 번째 요소로 이동한다.
- 출력문: p가 가리키는 두 번째 요소의 이름과 나이를 출력한다.
답안
Lee
38
7. 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오. (2021년 2회)
#include <stdio.h>
int mp(int base, int exp);
int main(void) {
int res;
res = mp(2, 10);
printf("%d", res);
return 0;
}
int mp(int base, int exp) {
int res = 1;
for (int i = 0; i < exp; i++) {
res = res * base;
}
return res;
}
답안
1024
8. 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오. (2021년 2회)
#include <stdio.h>
int main(void) {
int ary[3];
int s = 0;
*(ary + 0) = 1;
ary[1] = *(ary + 0) + 2;
ary[2] = *ary + 3;
for(int i = 0; i < 3; i++){
s = s + ary[i];
}
printf("%d", s);
}
답안
8
Python
1. 다음 Python 으로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오. (2020년 2회)
a={'일본', '중국', '한국'}
a.add('베트남')
a.add('중국')
a.remove('일본')
a.update({'홍콩', '한국', '태국'})
print(a)
답안
{'중국', '홍콩', '한국', '베트남', '태국'}
*순서 상관없이 집합 요소만 포함
2. 다음 출력 결과를 쓰시오. (2020년 4, 5회)
lol = [[1,2,3], [4, 5], [6, 7, 8, 9]]
print(lol[0])
print(lol[2][1])
for sub in lol:
for item in sub:
print(item, end="")
print()
답안
[1, 2, 3]
7
123
45
6789
3. 다음 Python으로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오. (2021년 1회)
class CharClass:
a = ['Seoul', 'kyeongi', 'Incheon', 'Daejeon', 'Daegu', 'Busan']
myVar = CharClass()
str01 = ''
for i in myVar.a:
str01 = str01 + i[0]
print(str01)
답안
SkIDDB
4. 다음 Python으로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오. (2021년 2회)
a = 100
result = 0
for i in range(1, 3):
result = a >> i
result = result + 1
print(result)
동작 설명
- 첫 번째 반복 (i = 1):
- result = a >> 1: 100을 오른쪽으로 1비트 시프트한다.
- 100의 이진수 표현은 01100100이다.
- 오른쪽으로 1비트 시프트하면 00110010가 되며, 이는 십진수로 50이다.
- result = 50 + 1: result는 51이 된다.
- 두 번째 반복 (i = 2):
- result = a >> 2: 100을 오른쪽으로 2비트 시프트한다.
- 100의 이진수 표현은 01100100이다.
- 오른쪽으로 2비트 시프트하면 00011001가 되며, 이는 십진수로 25이다.
- result = 25 + 1: result는 26이 된다.
답안
26
'[실기] 정보처리기사 > 프로그래밍 언어 활용' 카테고리의 다른 글
[SQL] 2017-2019 프로그래밍 문제 모음 (0) | 2024.07.15 |
---|---|
[C] 2017-2019 프로그래밍 문제 모음 (0) | 2024.07.14 |
[JAVA] 2017-2019 프로그래밍 문제 모음 (0) | 2024.07.13 |