Unit 2: Java Fundamentals Flashcards
True or False: Java is an object-oriented language
True
Objects can not be created without creating a blue-print associated to that object. This blue-print is known as…
Class
In Java, all codes are written inside…
Classes
True or False: class is not a keyword (reserved word)
False
What would the line of code “System.out.println(“Hello Everyone!”);” display and where would it display?
The code would display Hello Everyone! on the standard output (monitor)
True or False: System is a class
True
True or False: By convention, the first letter of each word in the class name is not capitalized
False
Define: Class
A class is a template, blueprint or contract that defines the attribute (data field) and methods (functionalities/behavior) associated with certain entities (the instances of that class)
Define: Object
An object is an instance of class. An instantiated object is given a name, and it is created/placed in the memory using the structure described within a class declaration
Computer can execute code in _________
Machine language
The extension of a java virtual object file is:
.class
The extension of a java source code file is:
.java
True or False: The source code in java is compiled into bytecode
True
True or False: Bytecode is machine dependent
False
True or False: In java, the main() method has to be declared as a static method of a class
True
In the following java statement, what does “System” represent:
System.out.println(“Welcome to Java!”);
A class
The name of a variable or object is used to:
Distinguish one variable from another
The type of a variable or object is used to:
Tell the memory how much this variable needs and what can be assigned to it
What type of statement is this:
float w_lb, w_kg
A declaration statement
What type of statement is this:
w_kg = w_lb * 0.454
An assignment statement
True or False: a+b/c+d is the same as a+(b/c)+d in java language
True
What value will be left over in this math statement:
3-8%5
Zero
What is the value of newNum after these statements:
int i = 10;
int newNum = 10 * i++;
100
True or False: The following two java statements perform the same operation:
regWages = regPay + overTime;
regPay + overTime = regWages;
False
True or False: The following two java statements will result in the same value for y:
y = a + b * c; y = b * c + a;
True
True or False: The following statement doubles the value stored in answer:
answer *= 2;
True
True or False: The following statement will evaluate to true if x is between 1 and 100 or the number is negative:
((x<100) && (x>1)) || x<0
True
What will the value of c be after the following statements:
int a = 3, b = 2;
double c;
c = (a+b)/2;
2.0
What will the value of c be after the following statements:
float a = 3, b = 2;
int c;
c = (a+b)/2;
The code will not compile
What will be the output of the following code:
int a = 2, b = 4;
a = ++b;
System.out.println(a+”, “+b);
5, 5
What will be the output of the following code:
boolean a = false;
a = 5 == 5;
System.out.println(a);
true
What will be the output of the following code:
int a;
System.out.println(a);
Compilation error will be generated
What will be the output of the following code:
int a = 2, b = a + 2; double b = 4;
System.out.println(a/b);
Compilation error will be generated
What will be the output of the following code:
int a = 2, float b = 4.0;
System.out.println(a/b);
Compilation error will be generated
Define: println() method
Displays a message or data to the standard output and moves the cursor to the next line
Define: print() method
Displays a message or data to the standard output
What do these backslash codes/escape sequences do, respectively:
\n, \t, ", ', \, \b
Creates a new line (like the enter key), tabs the argument, creates double quotations without interrupting the string, creates single quotations without interrupting the argument, creates a backslash, backspaces (identical to keyboard)
Describe the steps it takes to convert java into machine code
- Text editor
- Compiler
- Bytecode (virtual machine code)
- Java Virtual Machine (JVM)
Define: Compilation error (syntax error)
When the machine cannot execute the code because there is a grammatical error in the source code the machine cannot understand
Define: Runtime error (known as exception in java)
When the program environment detects an operation that is impossible to carry out when the program is running (e.g. integer divided by integer zero)
Define: Logical error (program bugs)
When a program does not perform the way it was expected to perform
Define: Identifier
Programmer defined names for variables, constants, classes, methods, namespaces, with specific characteristics
What are the characteristics an identifier is able to have?
Any letter (upper or lower case), any digits, and two special characters (_ and $)
What characteristics can an identifier not have?
It cannot start with a digit, cannot be a reserved word, and cannot include spaces
Define: float/double
Datatypes that store both fractional and whole numbers in the memory
Define: char
Datatype that stores single characters in the memory
Define: int
Datatype that stores only whole numbers in the memory
Define: boolean
Datatype that stores only true or false in the memory
What are the 8 primitive data types?
byte, short, int, long, float, double, boolean, char
What must be done first in a code in order to allow for data to be input from the keyboard and what is the code?
You must import the Scanner class from the java.util package
input java.util.Scanner;
or you could put… input java.util.*; for all classes in java.util package
What are the methods in the Scanner class?
nextByte();, nextShort();, nextInt();, nextLong();, nextFloat();, nextDouble();, next();, nextLine();, next().charAt(0);, nextLine().charAt(0);
Define: final
Assigns a value to a variable that cannot be changed later in the code. This is a modifier and it appears before the declaration of a datatype
What is the naming convention for constants?
All capital letters with underscores to act as spaces (e.g. final int MINIMUM_DRIVING_AGE = 16;)
Define: Java operator
Take one or more input values (known as operands) and produce one output value
List some java operators
+, -, *, /, =, ==, !=, ||, &&, >, =
Define: Unary, binary, and ternary operators respectively
Uses one operand, uses two operands, uses three operands
What would the following code display?
int j = 4, u = 11, m = u/j;
System.out.println(m);
2
Define: Modulo operator (%)
Displays the remainder of when one value is divided into another (e.g. 11%4.5 would display 2)
Describe what the output of the code below is and what happens to the variable:
int a = 9;
System.out.println(-a);
Displays: -9 The variable (a) is still 9
Describe what the output of the code below is and what happens to the variable:
boolean k = true;
System.out.println(!k);
Displays: false The variable (k) is still true
Describe what the output of the code below is and what happens to the variable:
int m = 9;
System.out.println(++m);
Displays: 10 The variable (m) is no longer 9 and becomes 10
Describe what the output of the code below is and what happens to the variable:
int n = 9;
System.out.println(–n);
Displays: 8 The variable (m) is no longer 9 and becomes 8
True or False: a += b; is the same as a = a + b;
True
What does this expression mean:
U and only U Are the best CLAss in the Univers
It is an easy way to remember operator precedence (Unary [pre-increments], Unary [negate, invert], Arithmetic, Comparison, Logical, Assignments, Unary [post-increments]
Assigning a value, of one data type to another, requires only one of two possible operations. What are the two possible operations?
Widening/implicit conversion, and narrowing/casting/explicit conversion
Define: Widening conversion (implicit conversion)
In an arithmetic expression involving two different datatypes, the final result of the expression will be stored using the operand with the highest data range in memory. It is automatic (e.g. int a, float b; (a+b); will be float type where int a gets promoted/widened to float a)
Define: Narrowing/explicit conversion (casting)
“Casts a spell” on a datatype to make it take up less data and converts it to a smaller datatype. It only works for whatever line the cast operator is on
What would the following code display:
int i = 4, j = 5, k = 9;
System.out.printf(“The addition of %d and %d is %d”,i,j,k);
The addition of 4 and 5 is 9
Define: Format specifiers
Used in the printf(); method to specify what datatype a variable is
What are some examples of format specifiers?
%b (boolean value), %c (character), %d (integer), %f (floating point number), %e (number in scientific notation), %s (string), %o (octal number), %x (hexadecimal number)
What are the 5 most frequent errors amongst all programmers?
- Undeclared/non-initialized variables
- Integer overflow (assign a variable a value outside of datatype’s range)
- Round-off errors (0.1 + 0.7 = 0.799999999999)
- Unintended integer division
- Redundant input objects
How do you write comments in 1036?
Write a summary at the top of the code to explain what the program does, key features, supporting data structures, and unique techniques it uses. Include name, class section, instructor, date, and a brief description at the beginning of the program. Include your own comments that may cause yourself confusion when self-reviewing later
What are the naming conventions for customizable names in java?
Variables or method names: Meaningful and descriptive names, all lowercase, concatenate the words if there are multiple word using camelcase
Class: Capitalize the first letter of the word, use camelcase
Constants: Capitalize all letters, use snakecase