Chapter 2: Elementary programming Flashcards
Data type
specifies the type of value stored in a variable(int, string, boolean, etc.)
Fundamental type
represents numbers, characters, and Boolean values
Variable
represents a stored value in a computers memory
Pseudocode
nature language + coding syntax(in this case Java)
double
used to declare a floating point number
char
used to declare a single character value such as “A”, “b”
String
used to declare multiple characters. like a sentence
What is a java object
A Java object is an instance of a class. Meaning that objects are formed under classes and have an identity, behavior, and a state.
import java.util.*;
Wildcard Import. imports all the classes in a package by using the asterisk as the wildcard.
Scanner input = new Scanner(System.in)
creates a Scanner object and assigns its reference to the variable ‘input’.
prompt
something that directs a user to enter input. A program should always tell a user what to enter when expecting input from the keyboard.
double radius = input.nextDouble();
this line will read the number (hopefully a number) inputed by the user and assign the value to ‘radius’.
IPO
Stands for input, process, and output and are the typical three steps for most simple programs in the text.
Wildcard import
imports all classes in the package by using the asterisk as the wildcard
Specific import
imports a specific/single class.
the correct method to read a real number is with a scanner object labeled ‘input’ is…
input.nextDouble()
identifiers
names of varaibles, classes interface, or packages
Identifiers must follow what rules?
- sequence of characters that consists of letters, digits, underscores, and dollar signs.
- an identifiers cannot start with a digit.
- Cannot be a reserve word
- Can be any length
Evaluate the statement:
“Every letter in a Java keyword is lowercase.”
True.
Which of the following are correct names for variables according to Java naming conventions?
- radius
- Radius
- RADIUS
- findArea
- FindArea
1 and 4.
Single word variables are in lowercase while multiple word variables follow “camelNamingConvention” in which the first word is lowercasae and the subsequent words have their first letter capitalized.
Constant
Represents a permanent data that never changes.
Constants are also known as what in Java…
Final ‘Variable’
what is the syntax for decalring a constant?
final datatype CONSTANTNAME = value;
Declare an int constant SIZE with value 20
final int SIZE = 20
Suppose m and r are integers. Write a Java expression for mr 2 to obtain a floating-point result.
- 0 * m * (r * r).
* using the floating point number 1.0 makes the product of the equation a floating point.*
Math.pow(2, 3) returns what type of datatype?
double: 8.0
Math.pow(4, 1 / 2) returns
1.0
Assume that an int variable x that has already been declared , and initialized to a non-negative value .
Write an expression whose value is the last (rightmost) digit of x.
public static void main(String[] args) {
int x = 14;
System.out.println(x % 10.0); }
the underlined section is the expression.
Declare and initialize the following variables:
int monthOfYear, initialized to the value 11
long companyRevenue, initialized to the value 5666777
int firstClassTicketPrice, initialized to the value 6000
long totalPopulation, initialized to the value 1222333
int monthOfYear = 11;
long companyRevenue = 5666777;
int firstClassTicketPrice = 6000;
long totalPopulation = 1222333;
What is a literal?
A constant value that appears directly in the program.
How would 1.23456 x 10^2 be written in Java?
1.23456e2 or 1.23456E2
How many accurate digits are stored in a floating and double point value?
A float contains 7-8 number of accurate digits whereas a double contains 15-17.
Which of the following are correct literals for floating-point numbers?
12.3, 12.3e+2, 23.4e-2, -334.4, 20.5, 39F, 40D
All are viable options.
Which of the following are the same as 52.534?
5.2534e+1, 0.52534e+2, 525.34e-1, 5.2534e+0
5.2534e+1, 0.52534e+2, 525.34e-1
Which of the following are correct literals?
5_2534e+1, _2534, 5_2, 5_
5_234e+1 & 5_2
Write a literal representing the Long value 12 billion
12000000000L
Write a hexadecamial integer literal with the value 15
0xf
The expression 4 + 20/(3-1) *2 is evaluated into…
24.
- (3-1) = 2
- 20/2 = 10
- 10 * 2 = 20
- 20 + 4 = 24
How to display the current UTC hour?
System.currentTimeMillis()/1000/60/60 %24
What is the output of…
double a = 6.5
a += a + 1
System.out.println(a)
14…
The code is saying a is equal to 6.5. Then the second line is saying “a” is assigned the value a = a + a + 1 which is 6.5 + 6.5 + 1.
So 14 would be printed out onto the terminal.
What is the output of…
a = 6;
a /= 2;
System.out.println(a)
3 will be printed on the terminal. The code, is regular written math/english would be written as…
a is equal to 6.
a is equal to 6 divided by 2.
Print the output of the value of “a”.
What is the value of x after the following statements:
int x = 2
int y = 1
x *= y + 1
x = 4
It may be tempting to say 3 but remeber, the augmented assignment operators are performed last in the order.
What is the output of the following code:
int a = 6;
int b = a++;
System.out.println(a);
System.out.println(b);
a = 6;
b = ++a
System.out.println(a)
System.out.println(b)
7
6
7
7
Preincrement
Increase the value by 1 AND return the NEW value.
Postincrement
Decrease the value by 1 AND return the OLD value.
Predecrement
Decrease the value by 1 AND return the NEW value.
Postdecrement
Decrease the value by 1 AND return the old value.
What are the operands in this expression:
result = a + b + c;
a,b,c
what are the operators in this expression;
result = a + b - c;
+,-
Are the statements equivalent?
number += 1;
number = number + 1;
number++;
++number;
YES. AS STATEMENTS THEY ARE THE SAME AS EXPRESSIONS THEY WOULD NOT BE.
What does “sum” become after this statement?
int sum = 0;
sum += 4.5;
sum becomes 4 since it is type int.
sum += 4.5; is the same as sum = (int)(sum + 4.5);
To assign a double varibale, d, to a float variable x, you write…
x = (float)d;
Which one of these assignment statements is illegal? Why?
float f = -34;
int t = 23;
short s = 10;
int t = 4.5;
int t = 4.5;
This is because you cannot assign a floating point number (larger datatype) to a smaller datatype (integer) without explicit type casting.
What will be the output of the following code:
System.out.println((double)(5/2));
2.0