본문 바로가기
자바(Java)/자바 AP

[자바AP 연습문제] 01.Primitive Types

by 긱펀 2024. 3. 21.
반응형

[자바AP 연습문제] 01.Primitive Types

문제의 정답은 제일 아래 "더보기" 클릭

 

*Unit.1 Primitive Types Summary를 복습하려면 아래 "더보기" 클릭

더보기

Unit.1 Primitive Types Summary

  • Compiler - Software that translates the Java source code into the Java class file which can be run on the computer.
  • Compiler or syntax error - An error that is found during the compilation.
  • Main method - Where execution starts in a Java program.
  • Variable - A name associated with a memory location in the computer.
  • Declare a Variable - Specifying the type and name for a variable. This sets aside memory for a variable of that type and associates the name with that memory location.
  • Initializing a Variable - The first time you set the value of a variable.
  • data type - determines the size of memory reserved for a variable, for example int, double, boolean, String.
    integer - a whole number like 2 or -3
  • boolean - An expression that is either true or false.
  • Camel case - One way to create a variable name by appending several words together and uppercasing the first letter of each word after the first word (myScore).
  • Casting a Variable - Changing the type of a variable using (type) name.
  • Operator - Common mathematical symbols such as + for addition and * for multiplication.
  • Compound assignment or shortcut operators - Operators like x++ which means x = x + 1 or x *=y which means x = x * y.
  • remainder - The % operator which returns the remainder from one number divided by another.
  • arithmetic expression - a sequence of operands and operators that describe a calculation to be performed, for example 3*(2 + x)
  • operator precedence - some operators are done before others, for example *, /, % have precedence over + and -, unless parentheses are used.
  • boolean - used to declare a variable that can only have the value true or false.
  • double - used to declare a variable of type double (a decimal number like 3.25).
  • int - used to declare a variable of type integer (a whole number like -3 or 235).
  • String - used to declare a variable of type String which is a sequence of characters or text.
  • System.out.print()- used to print output to the user
  • System.out.println() - used to print output followed by a newline to the user
  • = - used for assignment to a variable
  • +, -, *, /, % - arithmetic operators

 

*자주하는 실수에 대해 보려면 아래 "더보기" 클릭

더보기
  • forgetting that Java is case sensitive - myScore is not the same as myscore.
  • forgetting to specify the type when declaring a variable (using name = value; instead of type name = value;)
  • using a variable name, but never declaring the variable.
  • using the wrong name for the variable. For example calling it studentTotal when you declare it, but later calling it total.
  • using the wrong type for a variable. Don’t forget that using integer types in calculations will give an integer result. So either cast one integer value to double or use a double variable if you want the fractional part (the part after the decimal point).
  • using == to compare double values. Remember that double values are often an approximation. You might want to test if the absolute value of the difference between the two values is less than some amount instead.
  • assuming that some value like 0 will be smaller than other int values. Remember that int values can be negative as well. If you want to set a value to the smallest possible int values use Integer.MIN_VALUE.

 

[문제1]

Consider the following code segment. What is printed as a result of executing the code segment?

1
2
3
System.out.print("Java is ");
System.out.println("fun ");
System.out.print("and cool!");
cs

 

 

 

[문제2]

Consider the following code segment.
The code segment is intended to produce the following output but may not work as intended.
Which change, if any, can be made so that the code segment produces the intended output?

1
2
3
4
System.out.println("Roses are red, ")      // Line 1;
System.out.println("Violets are blue, ")  // Line 2;
System.out.print("Unexpected '}' ")        // Line 3;
System.out.print("on line 32. ")           // Line 4;
cs

 

A. Replacing print with println on lines 3 and 4.
B. Replacing println with print on lines 1 and 2.
C. Removing the single quotes in line 3.
D. Putting the semicolon after the ) on each line.

 

 

[문제3]

Which of the following pairs of declarations are the most appropriate to store a student’s average course grade in the variable GPA and the number of students in the variable numStudents?

A. int GPA; int numStudents;
B. double GPA; int numStudents;
C. double GPA; double numStudents;
D. int GPA; boolean numStudents;
E. double GPA; boolean numStudents;

 

 

[문제4]

What is printed when the code segment is executed?

1
2
3
4
int a = 5;
int b = 2;
double c = 3.0;
System.out.println(5 + a / b * c - 1);
cs

 

A. 0.666666666666667
B. 9.0
C. 10.0
D. 11.5
E. 14.0

 

 

[문제5]

The following code should print “Mary’s favorite color is blue”. However, the code has errors. Fix the code so that it compiles and runs correctly.

1
2
3
4
5
6
7
public class Main {
  public static void main(String[] args) {
    String name = Mary";
    String color = "blue"
    System.out.println(Name + "'s favorite color is " + color);
  }
}
cs

 

 

[문제6]

The following code should print Your name is Carly and your favorite color is red. Finish the code so that it prints the output correctly using the variables provided.

1
2
3
4
5
6
7
public class Main {
  public static void main(String[] args) {
    String name = "Carly";
    String color = "red";
    System.out.println();
  }
}
cs

 

[문제7]

Finish the code below so that it prints Your name is Justin and your age is 16 using the variables provided.

1
2
3
4
5
6
7
public class Main {
  public static void main(String[] args) {
    String name = "Justin";
    int age = 16;
    System.out.println();
  }
}
cs

 

 

[문제8]

The following code should calculate the cost of a trip that is 300 miles if gas is $2.50 a gallon and your car gets 30 miles per gallon. However, the code has syntax errors, like missing semicolons, wrong case on names, or unmatched " or (. Fix the code so that it compiles and runs correctly.

1
2
3
4
5
6
7
8
9
10
11
public class Main {
  public static void main(String[] args) {
    int tripMiles = 300
    Double price = 2.50;
    int milesPerGallon = 30;
    double numberOfGallons = tripmiles / milesPerGallon;
    double totalCost = numberOfGallons * price;
    System.out.println(totalCost);
 
  }
}
cs

 

 

[문제9]

What does the following code print?

 

 

[문제10]

What does the following code print?

1
System.out.println(1 / 3);
cs

 

 

[문제11]

Given the following code segment, what is the value of b when it finishes executing?

 

 

[문제12]

What will the following code print?

1
System.out.println(1.0 / 3);
cs

 

 

 


 

[문제 정답은 아래 "더보기" 클릭]

더보기

[문제1 정답]

D

(Pay attention to which lines are print or println.)

 

[문제2 정답]

D

(The semicolon should go after each statement but not in the comment.)

 

[문제3 정답]

B

(the average grade could be a decimal number, and the number of students is an integer.)

 

[문제4 정답]

C

(this is equivalent to (5 + ((a/b)*c) - 1).)

 

[문제5 정답]

1
2
3
4
5
6
7
public class Main {
  public static void main(String[] args) {
    String name = "Mary";
    String color = "blue";
    System.out.println(name + "'s favorite color is " + color);
  }
}
cs

 

[문제6 정답]

1
2
3
4
5
6
7
public class Main {
  public static void main(String[] args) {
    String name = "Carly";
    String color = "red";
    System.out.println("Your name is "+ name + " and your favorite color is "+ color);
  }
}
cs

 

[문제7 정답]

1
2
3
4
5
6
7
8
public class Main {
  public static void main(String[] args) {
    String name = "Justin";
    int age = 16;
    System.out.println("Your name is " + name + " and your age is " + age);
 
  }
}
cs

 

 

[문제8 정답]

1
2
3
4
5
6
7
8
9
10
public class Main {
  public static void main(String[] args) {
    int tripMiles = 300;
    double price = 2.50;
    int milesPerGallon = 30;
    double numberOfGallons = tripMiles / milesPerGallon;
    double totalCost = numberOfGallons * price;
    System.out.println(totalCost);
  }
}
cs

 

(Line 5 is missing a semicolon. Line 6 has Double instead of double. Remember that the primitive types all start with a lowercase letter. Line 8 has tripmiles instead of tripMiles. Remember that you should uppercase the first letter of each new word to make the variable name easier to read (use camel case).) 

 

[문제9 정답]

 A

(Whenever the first number is smaller than the second, the remainder is the first number. Remember that % is the remainder and 3 goes into 2 0 times with a remainder of 2.)

 

[문제10 정답]

 B

(When two integers are divided the results will also be integer and the fractional part is thrown away.)

 

[문제11 정답]

 D

(When a double is converted into an integer in Java, it truncates (throws away) the digits after the decimal.)

 

[문제12 정답]

 C

(The computer can not represent an infinite number of 3's after the decimal point so it only keeps 14 to 15 significant digits.)

 

 

 

 

 

*코딩 강의 문의: wootekken@naver.com

 

*코딩 강의 문의: wootekken@naver.com

728x90
반응형

댓글