Intro to Java Programming

Our Intro to Java Programming for Freshmen and Middle schoolers alike, covers comprehensive list of topics. Feel free to take a sample test below and Self Evaluate

  1. Introduction to Programming and Java
  2. Data Types
  3. Selections and Operators
  4. Mathematical Functions and Strings
  5. Loops
  6. Methods
  7. Objects and Classes
  8. Single Dimensional Arrays
  9. Introduction to Inheritance and Polymorphism

1. 
int sum = 0;
for(int i = 1, j = 10; i <= j; i++, j--) {
    sum += i + j;
}
System.out.println(sum); 

A) 55
B) 60
C) 65
D) 70

2.
int num = 5, count = 1;
while(count <= num) {
    num -= count;
    count++;
}
System.out.println(num); 

What will be printed to the console?

A) 0
B) 1
C) 2
D) 3

3.
int x = 30, y = 40;
if(x == y) {
    System.out.println("X equals Y");
} else if(x < y) {
    System.out.println("X is less than Y");
} else if(x > y) {
    System.out.println("X is greater than Y");
} 

A) X equals Y
B) X is less than Y
C) X is greater than Y
D) The code will not compile

4. 
char grade = 'B';
switch(grade) {
    case 'A':
        System.out.println("Excellent");
        break;
    case 'B':
        System.out.println("Good");
        break;
    case 'C':
        System.out.println("Average");
        break;
    case 'D':
        System.out.println("Deficient");
        break;
    case 'F':
        System.out.println("Failing");
        break;
    default:
        System.out.println("Invalid grade");
} 

A) Excellent
B) Good
C) Average
D) Invalid grade

5. 
What will happen when the following method is called with the argument 7
public static void mystery(int x) {
    x += 3;
    x *= 2;
    System.out.println(x);
}

A) It will print 14
B) It will print 17
C) It will print 20
D) It will print 22

6. What is the output of the following code snippet? 

int[] array = {1, 2, 3, 4, 5}; for(int i = 0; i < array.length; i++) {
if(i % 2 == 0) { array[i] = i; }
System.out.print(array[i] + " "); }

A) 0 2 2 4 4

B) 1 2 3 4 5
C) 0 2 3 4 5
D) 1 1 3 3 5

Java

7. What will be the output of the following Java method if calculate(2, 5) is called?public static int calculate(int a, int b) {
    return a + b * (a - b);
}

A) -6
B) 11
C) 13
D) -13

8. Given the method below, what will processArray(new int[]{1, 2, 3, 4, 5}} return?
public static int processArray(int[] array) {
    int sum = 0;
    for(int i : array) {
        sum += i;
        if(i % 2 == 0) {
            sum -= 3;
        }
    }
    return sum;
}

A) 9
B) 10
C) 11
D) 12

9. If the following method is executed with the input printPattern(4), what will it print?
public static void printPattern(int n) {
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= i; j++) {
            System.out.print("*");
        }
        System.out.println();
    }
}

A)

*
**
***
****

B)

**
*
C) No output

10. What does the following method return when findMax(new int[]{3, 5, 7, 2, 8}) is called?
public static int findMax(int[] numbers) {
    int max = Integer.MIN_VALUE;
    for(int num : numbers) {
        if(num > max) {
            max = num;
        }
    }
    return max;
}

A) 2
B) 5
C) 7
D) 8

11. Consider the following Java method. What will reverseString("hello") return?
public static String reverseString(String str) {
    String reversed = "";
    for(int i = str.length() - 1; i >= 0; i--) {
        reversed += str.charAt(i);
    }
    return reversed;
}

A) olleh
B) hello
C) oellh
D) eholl

Key

  1. A
  2. C
  3. B
  4. B
  5. C
  6. A
  7. D
  8. A
  9. A
  10. D
  11. A
Scroll to Top