< C++ >
# 1. 다음 C언어의 출력값을 작성하시오.
[소스코드]
#include <stdio.h>
int main()
{
char a[] = "Art";
char* p = NULL;
p = a;
printf("%s\n", a);
printf("%c\n", *p);
printf("%c\n", *a);
printf("%s\n", p);
for (int i = 0; a[i] != '\0'; i++)
printf("%c", a[i]);
return 0;
}
문자형 배열 a를 "Art"로 초기화
포인터 p를 NULL로 초기화
p를 a로 초기화 // a의 배열 첫 번째 요소를 가리킴
[출력결과]
Art
A
A
Art
Art
# 2. 다음 C언어의 출력값을 작성하시오.
[소스코드]
#include <stdio.h>
int main()
{
const char* a = "qwer";
const char* b = "qwtety";
for (int i = 0; a[i] != '\0'; i++)
{
for (int j = 0; b[j] != '\0'; j++)
{
if (a[i] == b[j]) printf("%c", a[i]);
}
}
return 0;
}
포인터 a를 문자열 리터럴 "qwer"로 초기화
포인터 b를 문자열 리터럴 "qwtety"로 초기화
반복문의 구조를 보면 배열 a, b의 인덱스를 순회하여 동일한 문자가 존재하면 출력
[출력결과]
qwe
# 3. 다음 아래 코드에서 이진수를 십진수로 변환하는 코드에 대해 괄호 (a) (b)의 적합한 답을 작성하시오.
[소스코드]
#include <stdio.h>
int main()
{
int input = 101110;
int di = 1;
int sum = 0;
while (1)
{
if (input == 0)
break;
else
{
sum = sum + (input(a)(b)) * di;
di = di * 2;
input = input / 10;
}
}
printf("%d", sum);
return 0;
}
else문 안의 sum은 이진수 1의 자리부터 이진 합을 구하기 때문에, input % 10을 하여 나머지와 di를 곱연산
# 4. 다음 코드에서 괄호 안에 알맞은 값을 변수명으로 작성하시오.
[소스코드]
#include <stdio.h>
void swap(int a[], int idx1, int idx2)
{
int t = a[idx1];
a[idx1] = a[idx2];
a[idx2] = t;
}
void sort(int a[], int len)
{
for (int i = 0; i < len; i++)
for (int j = 1; j < len - i - 1; j++)
if (a[j] > a[j + 1])
swap(a, j, j + 1);
}
int main()
{
int nx = 5;
int a[] = { 5, 15, 7, 20, 11 };
sort(a, nx);
for (int i = 0; i < nx; i++)
printf("%d ", a[i]);
return 0;
}
위 문제의 알고리즘은 버블정렬 문제이다
swap 메서드는 파라미터로 배열 a, 정수 idx1, idx2를 전달받는다
전달받은 파라미터값으로 정수 t를 a[idx1]로 초기화
a[idx1]을 a[idx2]로 초기화
a[idx2]를 t로 초기화
즉, 배열의 idx1번째 요소와 idx2번째 요소의 위치를 바꾸는 메서드라고 보면 된다
sort 메서드는 파라미터로 배열 a, 정수 len을 전달받는다
전달받은 파라미터값으로 반복문을 순회하는데 이는 배열의 요소를 순서대로 정렬해 주는 버블정렬 알고리즘 메서드다
정수 nx를 5로 초기화
배열 a의 요소를 {5, 15, 7, 20, 11}로 초기화
sort(a, nx) // 간단 명료하게 두 개의 반복문을 통해 해당하는 배열의 인덱스의 인접하는 두 개의 요소를 비교하여 swap 메서드를 호출
[출력결과]
5 7 11 15 20
< Java >
# 1. 아래 자바 코드에서 출력되는 값을 작성하시오.
[소스코드]
class Static
{
public int a = 20;
static int b = 0;
}
public class good
{
public static void main(String[] args)
{
int a;
a = 10;
Static.b = a;
Static st = new Static();
System.out.println(Static.b++);
System.out.println(st.b);
System.out.println(a);
System.out.print(st.a);
}
}
정수 a 선언
a를 10으로 초기화
Static 클래스의 변수 b는 static으로 정의되었기 때문에 생성자를 통해 객체를 생성하지 않아도 접근이 가능
Static.b를 10으로 초기화
Static타입 st를 생성자를 호출하여 새로운 객체를 생성
Static.b++ // 10
st.b // 11
a // 10
st.a // 20
[출력결과]
10
11
10
20
# 2. 다음 JAVA 코드에서 알맞는 출력 값을 작성하시오.
[소스코드]
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 good {
public static void main(String[] args) {
Vehicle obj = new Car("Spark");
System.out.println(obj.getName());
}
}
Car클래스의 생성자를 호출하고 "Spark"를 파라미터로 전달하여 새로운 객체를 생성
obj.getName()은 먼저 Car클래스의 getName() 메서드를 찾는데, Car클래스의 getName은 모두 파라미터를 필요로 하기 때문에 부모클래스인 Vehicle클래스의 getNaem() 메서드 호출
[출력결과]
Vehicle name: Spark
# 3. 다음 자바 코드에 대한 출력 값을 작성하시오.
[소스코드]
class Parent
{
int x = 100;
Parent()
{
this(500);
}
Parent(int x)
{
this.x = x;
}
int getX()
{
return x;
}
}
class Child extends Parent
{
int x = 4000;
Child()
{
this(5000);
}
Child(int x)
{
this.x = x;
}
}
public class good
{
public static void main(String[] args)
{
Child obj = new Child();
System.out.println(obj.getX());
}
}
Child클래스의 생성자를 호출하여 새로운 객체를 생성
Child()
this(5000)
Child(5000)
Parent()
this(500)
this.x = 500 // Parent클래스 int x = 500으로 초기화
this.x = 5000 // Child클래스 int x = 5000으로 초기화
obj.getX()
자식 클래스에는 getX() 함수가 존재하지 않기 때문에, 부모 클래스의 getX()를 호출
[출력결과]
500
'정보처리기사' 카테고리의 다른 글
[2023년 3회] (0) | 2024.10.07 |
---|---|
[2023년 2회] (0) | 2024.10.07 |
[2022년 3회] (0) | 2024.10.04 |
[2022년 2회] (0) | 2024.10.04 |
[2022년 1회] (0) | 2024.10.04 |