APCSA Unit 1: Primitive Types Exam

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Naming Variable Rules

- We can only use letter/number characters and the special characters _ and $. No spaces or other characters are allowed. - Your variable can't start with a number (so num1 is ok as a variable name but 1num is not). - You can't use names which already have a meaning in Java (e.g. we can't name our variable String or System).

What is output when the following code is executed? double num = (0 - 5) * 3; System.out.println(num);

-15.0

Adding Comment

//Comment or /* another type of commentt */

What does the following print out? int x = 9; x++; System.out.println(x);

10

What is output when the following code is executed? double num = 2.0 * (46.75 - 40); System.out.println(num);

13.5

System.out.println(2 % 9);

2

Which of the following arithmetic expressions evaluates to 1 ? 1. 2 / 5 % 3 2. 2 / (5 % 3) 3. 2 / 5 + 1

2 & 3 Only: Expression I evaluates to 0 since 2 / 5 evaluates to 0 and 0 % 3 also evaluates to 0. Expression II evaluates to 1 since 5 % 3 evaluates to 2 and 2 / 2 evaluates to 1. Expression III evaluates to 1 since 2 / 5 evaluates to 0 and 0 + 1 evaluates to 1.

Consider the code segment below. int x = 10; int y = 20; System.out.print(y + x / y); What is printed as a result of executing the code segment?

20 The integer division x / y is performed first, resulting in 0. Then the sum of 20 and 0, or 20, is printed.

What will the following print out? int x = 9; int y = 2; System.out.println(x / y);

4

int x = 7; int y = 9; int z = 2; System.out.println(y / z);

4

What will the following print out? int x = 11; int y = 2; double z = x/y; System.out.println(x / y);> 4

5.0 Even though Java knows that z is going to be a double, it's still only looking at x and y, which are both integers. We need to add a double to the calculation

int x = 11; int y = 2; double z = 1.0 * x/y ; System.out.println(z);

5.5

What does the following print out? int x = 4; x += 2;

6

What does x equal after the code has been executed? int x = 13; x = x / 2;

6

What does the following print out? int x = 9; x--; System.out.println(x);

8

Declaring a Variable

A statement that defines a variable with a data type

Scanner

Allows for user input

double x = 7 / 2; // x has value 3, but no error is thrown double y = (double) 7 / 2; // y has value 3.5

Casting in an expression actually creates a temporary value that will be used when evaluating the expression.

What happens if you divide by 0

Dividing by 0 will cause an ArithmeticException error. Example: int x = 5; int y = 0; int z = x / y; // This line will cause an error

Truncating

Double -> Int. it will chop off the extra numbers and decimals

Consider the following code segment: System.out.println(hello); // Line 1 System.out.print(world); // Line 2 The code segment is intended to produce the following output but does not work as intended: hello world

Enclosing hello in line 1 and world in line 2 in quotation marks

What does this print: System.out.print("Hello"); System.out.println("World");

HelloWorld

Implicit Casting

Java will convert values that are safe for us. Programmers call this implicit casting because we don't specify the change to Java.

Explicit Casting

Java won't convert unsafe values unless we explicitly tell it to make the change. That's explicit casting because we tell Java to make the change even if it is not safe. If the value in the wider type doesn't fit into the narrower type, Java just cuts off the part that doesn't fit (truncates). For example if we cast the double 7.8 to an int, it becomes 7 and the .8 is cut off (never rounded and all memory of the .8 is gone). So be very careful with explicit casting.

Enter 2 integers and print out the calculated average

Scanner scan = new Scanner(System.in); System.out.println("Please enter two integers: "); int num1 = scan.nextInt(); int num2 = scan.nextInt(); double answer = (double)(num1+num2)/(2); System.out.println(answer);

What is wrong with the following code? String s = nextLine();

Should be: String s = scan.nextLine();

Consider the following code segment, which is intended to display 0.5. int num1 = 5; int num2 = 10; double ans = num1 / num2; System.out.print(ans); Which of the following best describes the error, if any, in the code segment?

The code should have cast either num1 or num2 in the expression num1 / num2 to double. For the code to display 0.5, at least one of the operands in the expression num1 / num2 must be cast to double. Otherwise, dividing an int with a value of 5 by an int with a value of 10 will result in an int with a value of 0.

int x = 0 System.out.println(9 % x);

This will cause a Arithmetic Exception

Will the following program work? int a = 5;double x = 4.2;int b = 2 * a; // b is set to 10double y = x + 3.2; // y is set to 7.4

Yes

Strings

anything that is not used for numerical calculations and can contain any kind of symbol, including letters, numbers, and special characters.

Different data types can store different amounts of data. In the options below, which has the data types listed in order, from smallest to largest?

boolean, int, double, String

What is casting

changes a variable from one data type to another

What data type should you use to hold the average of your test scores?

double

Converting minutes to hours & minutes

double min = time % 60; int mins = time%60; int x = time-mins; int hours = x/60;

How do you scan for a double?

double y = scan.nextDouble();

Divide by 10 gets you the

first number

Consider the following program which is intended to get the amount of time (in minutes) it took someone to do the chores and then converts it to hours and minutes. Scanner scan = new Scanner(System.in); int h = 0; int m; System.out.print("How many minutes did it take you to do your chores? "); m = scan.nextInt(); /* missing code */ System.out.println("It took you " + h + " hours and " + m + " minutes.");

h = m / 60; m = m % 60;

Which line of code properly casts a double to an integer?

int x = (int) 3.14;

Which line of code correctly declares and initializes the variable x to a value of 4?

int x = 4;

How do you scan for an Int?

int x = scan.nextInt();

What does System.out.print() do

prints to the monitor

What does System.out.println() do

prints to the monitor, and then prints a blank line

int

small in memory, stores whole number

reference data type

store a memory reference to where the data is stored.

Primitive data type

stored directly in a variable

double

stores a decimal number and takes up more space

Mod by 100 divide by 10 gets you

the middle number in a triple digit number

boolean

used for storing true or false values. Not words or letters.

The volume of a cylinder is equal to the height times the area of the circular base. The area of the circular base is equal to π (pi) times the square of the radius. The code segment below is intended to compute and print the volume of a cylinder with radius r and height h. Assume that the double variables r, h, and pi have been properly declared and initialized. /* missing code */ System.out.print(volume); Which of the following can be used to replace /* missing code */ so that the code segment works as intended? 1. double baseArea = pi * r * r; double volume = baseArea * h; 2. double volume = pi * r * r; volume = volume * h; 3. double volume = pi * r * r * h;

1,2, & 3 Code segment I correctly calculates baseArea as pi * r * r, then correctly calculates volume by multiplying baseArea by h. Code segment II calculates the base area of the cylinder and temporarily stores it in volume. This value is multiplied by h, resulting in the correct volume. Code segment III correctly calculates the volume by multiplying pi * r * r * h in a single expression.

Mod by 10 gets you the

ones digit

What number does the program print when compiled and run? int y = 2147483647; y = y + 1; System.out.println(y);

-2147483648 This is called "overflow." It may seem weird, but it's similar to an old-style car odometer that "flips" over after getting too big. When there aren't any more bits to store the number, and you add one more to it, you "roll it over" and get a very large negative number. Try adding more to the variable and see what it does.

int x = 7; int y = 9; int z = 2; System.out.println(x % z);

1

int x = 7; int y = 9; int z = 2; System.out.println(y % z);

1

Rounding Numbers: What does the following print out? double num = 2.8; int roundNum = (int) (num + 0.5); System.out.println(roundNum);

3

What does the following print out? int x = 3; x *= 7; x -= 3; x /= 6; System.out.println(x);

3

int x = 7; int y = 9; int z = 2; System.out.println(x / z);

3

Consider the following code segment, which is intended to display 6.0. double fact1 = 1 / 2; double fact2 = 3 * 4; double product = fact1 * fact2; System.out.println(product); Which of the following best describes the error, if any, in the code segment?

Either the numerator or the denominator of the fraction 1 / 2 should be cast as double. By default, since 1 and 2 are int types, the division will also return an int type, and in integer division, 1 divided by 2 is 0. This result is stored in the double variable fact1 as 0.0. By casting the denominator (or the numerator) to double, the division will result in 0.5, and the rest of the code will work as intended.

How does a scanner look like

Scanner scan = new Scanner(System.in);

Write a program which takes a positive two digit integer as an input. The program should print out the digits one per line in order.

Scanner scan = new Scanner(System.in); System.out.println("Please enter a two digit number:"); int x = scan.nextInt(); System.out.println("Here are the digits: "); System.out.println(x / 10); System.out.println(x % 10);

Write a code that when you put in a 3 digit number, the numbers will be printed out on the screen backwards

Scanner scan = new Scanner(System.in); System.out.println("Please enter a three digit number: "); int number1; int number2; int number3; int x = scan.nextInt(); System.out.println("Here are the digits: "); System.out.println(x % 10); System.out.println(x % 100/10); System.out.println(x / 100);

Input a decimal number and have the code print out the 2 numbers the

Scanner scan = new Scanner(System.in); System.out.println("Please input a decimal number: "); double decimal = scan.nextDouble(); decimal = (int)(decimal * 100); int firstNum = (int)((decimal / 10) % 10); int secondNum = (int)((decimal % 10)); System.out.println(firstNum + " " + secondNum);

Round a decimal

Scanner scan = new Scanner(System.in); System.out.println("Please input a decimal number: "); double x = scan.nextDouble(); int decimalnum = (int)(x + 0.5); System.out.println("Answer: " + decimalnum);

How does combining strings look like

System.out.println("Hello " + n + " how are you?");> Hi John how are you?

Consider the following code segment, which is intended to calculate the average of two quiz scores. double avg = 15 + 20; avg /= 2; Which of the following best describes the behavior of the code segment?

The code segment stores 17.5 in avg because 17.5 is the result of the floating point division of 35.0 by 2. Because avg is a double variable, the result of the floating-point division operation avg /= 2 is also a double.

What does String n; do

This tells the computer to allocate space for a String and call that space 'n'

System.out.println("Welcome to AP Computer Science"); System.out.println("Programming in Java");

Welcome to AP Computer Science Programming in Java

What is wrong with this code: String s; System.out.println(s);

You cannot print a String without setting its value.

Which of the following is a legal variable name in Java?

name Can't be: 45house the name int this.is.a.variable


Kaugnay na mga set ng pag-aaral

Generation of an Action Potential

View Set

Bonus Chapter A Working within the Legal Environment

View Set

Chapter 27 programmable controllers

View Set

Fizx First Fall Final Study Guide

View Set