AP Computer Science FRQ Soru Örnekleri ve Açıklamaları

AP Computer Science sınavı için açıklamalı olarak FRQ soru örnekleri (AP Computer Science FRQ Example)

>Soru : 1 Aşağıdaki Kod Bloğu ne işe yarar? (What does the Code Block below do?)

public class Main {
    public static void main(String[] args) {
        int[] numbers = {1, 2, 3, 4, 5};
        int target = 5;
        int result = search(numbers, target);
        System.out.println("Target found at index: " + result);
    }

    public static int search(int[] arr, int target) {
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == target) {
                return i;
            }
        }
        return -1;
    }
}

Soru 2: Aşağıdaki kod parçasını inceleyin: (Check out the code snippet below:)

public class Main {
    public static void main(String[] args) {
        String str = "Hello World";
        System.out.println(reverseString(str));
    }

    public static String reverseString(String s) {
        String reversed = "";
        for (int i = s.length() - 1; i >= 0; i--) {
            reversed += s.charAt(i);
        }
        return reversed;
    }
}

Yukarıdaki kod parçası hangi çıktıyı verir? (What output does the above code snippet give?)

Soru 3 : Aşağıdaki kod parçasını inceleyin: (Check out the code snippet below:)

public class Main {
    public static void main(String[] args) {
        int[][] matrix = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        System.out.println("Sum of matrix elements: " + sumMatrix(matrix));
    }

    public static int sumMatrix(int[][] matrix) {
        int sum = 0;
        for (int i = 0; i < matrix.length; i++) {
            for (int j = 0; j < matrix[i].length; j++) {
                sum += matrix[i][j];
            }
        }
        return sum;
    }
}

Yukarıdaki kod parçası hangi çıktıyı verir? (What output does the above code snippet give?)

Soru 4 : Aşağıdaki kod parçasını inceleyin: (Check out the code snippet below:)

public class Main {
    public static void main(String[] args) {
        System.out.println(isPalindrome("racecar"));
        System.out.println(isPalindrome("hello"));
    }

    public static boolean isPalindrome(String str) {
        int left = 0;
        int right = str.length() - 1;
        while (left < right) {
            if (str.charAt(left) != str.charAt(right)) {
                return false;
            }
            left++;
            right--;
        }
        return true;
    }
}

Yukarıdaki kod parçası hangi çıktıyı verir?(What output does the above code snippet give?)

CEVAPLAR : (ANSWER)

Soru 1:

Bu soruda, bir dizide belirli bir hedef değeri arayan bir metodun çalışması test ediliyor. search adlı bir metot tanımlanmıştır. Bu metot, verilen bir tamsayı dizisinde hedef tamsayının indeksini bulmak için kullanılır. Eğer hedef tamsayı dizide bulunursa, indeksi döndürür, bulunamazsa -1 döndürür. Verilen dizideki hedef tamsayıyı bulmak için bir döngü kullanır.

Soru 2:

Bu soruda, bir string’in tersini bulan bir metotun çalışması test ediliyor. reverseString adlı bir metot tanımlanmıştır. Bu metot, verilen bir string’in tersini bulmak için kullanılır. Verilen string’in her bir karakterini sondan başa doğru alıp yeni bir string oluşturur.

Soru 3:

Bu soruda, bir matrisin elemanlarının toplamını hesaplayan bir metotun çalışması test ediliyor. sumMatrix adlı bir metot tanımlanmıştır. Bu metot, verilen bir matrisin tüm elemanlarının toplamını hesaplamak için kullanılır. İki tane for döngüsü kullanarak, matrisin her bir elemanını toplamak için dolaşır.

Soru 4:

Bu soruda, bir string’in palindrom olup olmadığını kontrol eden bir metotun çalışması test ediliyor. isPalindrome adlı bir metot tanımlanmıştır. Bu metot, verilen bir string’in palindrom olup olmadığını kontrol etmek için kullanılır. String’in başı ve sonu karşılaştırılarak palindrom olup olmadığı belirlenir. Baş ve son karakterler aynı değilse, string palindrom değildir.

Question 1:

In this question, the functionality of a method that searches for a specific target value in an array is being tested. A method named search is defined, which is used to find the index of the target integer in a given integer array. If the target integer is found in the array, it returns its index; otherwise, it returns -1. It uses a loop to find the target integer in the given array.

Question 2:

In this question, the functionality of a method that finds the reverse of a string is being tested. A method named reverseString is defined, which is used to find the reverse of a given string. It iterates through each character of the given string from end to start and creates a new string.

Question 3:

In this question, the functionality of a method that calculates the sum of elements in a matrix is being tested. A method named sumMatrix is defined, which is used to calculate the sum of all elements in a given matrix. It traverses through each element of the matrix using two nested for loops to calculate the sum.

Question 4:

In this question, the functionality of a method that checks whether a string is a palindrome or not is being tested. A method named isPalindrome is defined, which is used to check if a given string is a palindrome. It determines whether the string is a palindrome by comparing the characters at the beginning and end. If the characters at the beginning and end are not the same, the string is not a palindrome.