Java language basics Flashcards

1
Q

Data types specify….

A

specify the kind of thing a value represents

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Variables are….

A

used to store values for later use. must have a specified data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Arrays are….

A

used to store multiple values in a single variable which all must be the same data type

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Conditional Statements allow…..

A

Allow for branching and looping in Java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

entry point for the program to begin

A

main method - every java program must have one

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

main method syntax

A

public static void main (String[] args) {
//code goes here
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Class names should be _________ case?

A

PascalCase

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

One of the Class names much match the?

A

filename.java

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Sequence Diagrams show…..

A

show how application components or separate applications interact with each other and in the order of interactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Sequence Diagrams are structured…..

A

are structured to represent a timeline beginning at the top and descending to depict the sequence of interactions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Class diagrams are used…..

A

are used to describe classes and their relationships

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Class diagrams show….

A

the names and attributes of the classes, relationships between the classes, and sometimes also the methods of the classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What do the plus and minus signs indicate in this class diagram

A

+ before the attribute name means the attribute is public
- means the attribute is private so it is not visible to outside sources

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What do the words following the colons represent

A

In a class diagram this item when following attributes, it’s the data type for the attribute. When following methods, it’s the return type for the methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the class name?

A

Person

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What data type does setName return?

A

None the void means it doesn’t return anything

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What are the items below the second line?

A

This row of the diagram is a section containing the Constructors and Methods. Constructors are listed first and then methods.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What are the items at the top of this sequence diagram

A

the customer object who is outside our diagram and the theater and server objects inside our diagram

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the vertical dashed line?

A

A lifeline that represents the different objects or parts that interact over time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What are the solid horizontal lines?

A

Messages being sent between objects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What are the dashed horizontal lines?

A

Direct response messages that are the return for a message that was sent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is the vertical rectangles/activation boxes in some sequence diagrams?

A

The Activation box is present under the object next to the code for each process which the object is involved.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

what is casting?

A

The conversion of one data type to another

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

what is the memory size order from smallest to biggest of these data types:
int
float
byte
double
long
short

A

byte < short < int < long < float < double

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

what is the syntax for casting a larger data type (float) into a smaller(int) (Narrowing Casting)

A

float floatVar = 45.67
int intVar = int(floatVar)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

What is the size and range of values of a byte?

A

byte | 8-bit | -128 to 127

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What is the size and range of values of a short?

A

short | 16-bit | -32,768 to 32,767

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What is the size and range of values of a int?

A

int | 16-bit | -2 billion to 2 billion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is the size and range of values of a long?

A

long | 32-bit| -9 quintillion to 9 quintillion

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is the size and range of values of a float?

A

float | 32-bit | 6 decimal digits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is the size and range of values of a double?

A

double | 64-bit | 15 decimal digits

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

What is the size and range of values of a char?

A

char | 16-bit |single character

33
Q

What is the size and range of values of a boolean?

A

boolean | 1-bit | true or false

34
Q

What is a wrapper class?

A

A corresponding class to the primitive data types that allow us to define the primitive data type using that class instead.

35
Q

what is the Java.lang package?

A

A package that loads automatically with Java.

36
Q

What are the hundreds of included classes in the java language called?

A

Packages

37
Q

What separator do java package paths use?

A

Unlike folders, Java package paths use dots (.) as separators instead of slashes (\ or /).

38
Q

What java package is imported by default and contains built-in classes like String?

A

Java.lang (lang is short for language)

39
Q

When you want to use a class that’s part of a package what the common way?

A

The common way is to add an import statement to your class file where the other class will be used. This allows you to only use the class name in your code versus the full package name.

40
Q

What will this line of code do? import java.util.*;

A

It will import all of the classes in the java.util package for use.

41
Q

Why is code line a) preferred over b) when we only need the Scanner class?
a) import java.util.Scanner;
b) import java.util.*;

A

Importing only what you need will lead to improved maintainability, the readability of your code, and you won’t have problems with ambiguous class names (2 classes with the same name in 2 different packages).

42
Q

Where do the lines importing packages/classes go in your code?

A

At the very top.

43
Q

What is the difference between ++i and i++?

A

++i increases the value of i by 1 before executing the code on the line.
i++ increases the value of i by 1 after executing the code on the line.

44
Q

What do the dots represent when referring to packages?

A

The dots represent subdirectories.

45
Q

Why should we list the specific class to import and not use the star import (*)?

A

Better readability
Reduced errors

46
Q

What is a jar file?

A

A jar file is like a zip file that contains complied code. Java can search inside jar files and use the classes included, without expanding the archive on the disk.

47
Q

What is reference equality?

A

When two references point to the same object.

48
Q

What is the difference between equality and equivalence when comparing objects?

A

Equality check is the objects point to the same memory location. Equivalence checks to see if the object have the same variables and data.

49
Q

what is the syntax to compare stringOne and stringTwo?

A

stringOne.equals(stringTwo);

50
Q

What is the syntax to check if one object is equal to another?

A

objectOne == objectTwo
This only returns true if both of the objects are assigned to the same object.

51
Q

What is a simple explanation of a hashCode?

A

It is an integer used to quickly search for objects on Java.

52
Q

When you override the equals method you also need to override the……..

A

When you override the equals method, you also need to override the hashCode method.

53
Q

What does overriding the “equals” method do?

A

I allows us to establish equivalence between two objects. That way the “equals” method will return true even if == returns false, meaning you are dealing with two separate, but equivalent, objects.

54
Q

What are the six steps to overriding .equals() for the class City?

A

Check for self-reference: Check if the object being compared is the same instance as this object. If it is, return true, since an object is always equal to itself.

Check for null: Begin by checking if the object being compared is null. If it is, return false, since a null object cannot be equal to any other object.

Check for class compatibility: Check if the object being compared is an instance of the same class as this object. If it is not, return false, since objects of different classes cannot be equal.

Cast the object: If the object being compared is an instance of the same class, cast it to the appropriate type.

Compare object fields: Compare the fields of the object being compared to the fields of this object. Use the appropriate comparison method for each field type (for example, == for primitive types, and equals() for objects).

Return the result: Return true if all fields are equal, or false otherwise.

55
Q

Use this card to memorize the syntax for overriding the equals method in a class

A

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && name.equals(person.name);

56
Q

Create the equals method that does the following:
1) Take in an Object class argument and call it spare.
2) Check if the current object and the spare object are the same object by returning true if the current object (this) is equal to (using ==) as other.
3) Check if the spare object is null by returning false if other is null.
4) Check if the spare object is of the same class as the current object by returning false if getClass() is not equal to (using !=) to spare.getClass();.
5) Check if the spare object’s fullName and trueAge attributes are the same as the current objects by:
First casting the spare object into a Person class.
Comparing if the current objects fullName and trueAge are equal to the cast object’s fullName and trueAge.

A

@Override
public boolean equals(Object spare) {
if (this == spare) return true;
if (spare == null) return false;
if (getClass() != spare.getClass()) return false;
Person sparePerson = (Person) spare ;
return fullName.equals(sparePerson.fullName) && trueAge == sparePerson.trueAge);

OR
@Override
public boolean equals(Object spare) {
if (this == spare) return true;
if ((spare == null) || (getClass() != spare.getClass()))
return false;
Person sparePerson = (Person) spare ;
return fullName.equals(sparePerson.fullName) && trueAge == sparePerson.trueAge);

57
Q

a and b are two objects, and a.equals(b) returns true. Also, a.hashCode() returns 1234, what will be returned by b.hashCode()? Assume that both implement equals and hashCode methods correctly.

A

1234 – Equal objects have equal hash codes. Since a and b are equal, and the hash code of a is 1234, the hash code of b will also be 1234.

58
Q

What will happen when you compile and run the following code?
String s1 = “Manish”;
String s2 = s1;
s1 = null;
System.out.println(s2.equals(s1));

A

false – Whenever null is passed as an argument to the equals method which is invoked on a non-null reference; it always returns false. In this code, s2 refers to the String “Manish”, and s1 becomes null. Hence the call s2.equals(s1) returns false.

59
Q

What will be printed when the following code is compiled is run? Type only the output that will printed in correct case without any quotes or spaces.
Object i = new Integer(7);
Object f = new Float(7);
Object d = new Double(7);
System.out.println(i.equals(f) == d.equals(f));

A

true – All the wrapper classes return false if the argument of the equals method is of different type. i.e. In the case of all wrapper classes, a.equals(b) will return false if a and b are objects of different classes, irrespective of the value they represent. Here, the calls i.equals(f) and d.equals(f) both return false, and comparing two false with an equality operator (==) prints true.

60
Q

What is the code for importing Java’s built in hashCode method?

A

import java.util.Objects;

61
Q

What is the code to use the variables name and age to override the hashCode using Objects.hash?

A

public int hashCode() {
return Objects.hash(name, age);
}

62
Q

What’s the first if statement code inside the method: public boolean equals(Object o) {
}

A

if (o == this) {
return true;
}

63
Q

What’s the 2nd if statement code inside the method: public boolean equals(Object o) {
}

A

if (o == null) {
return false;
}

64
Q

What’s the 3rd if statement code inside the method: public boolean equals(Object o) {
} (note the class is Person)

A

if (!(o instanceof Person)) {
return false;
}

65
Q

What’s the last statement code inside the method: public boolean equals(Object o) {
} (note the class is Person with the variables name and age)

A

Person other = (Person) o;
return this.name.equals(other.name) && this.age == other.age;
}

66
Q

What does the .equals() method compare by default?

A

By default, the equals method in Java compares the memory addresses of two objects to determine if they are equal. If two objects are of different classes, they will have different memory addresses and therefore equals will return false.

67
Q

What’s wrong with this initialization: float myfloat = 3.14;

A

An f needs to be added: float myfloat = 3.14f;
When you initialize a float variable with a floating-point literal, the compiler needs to know whether the value should be interpreted as a float or a double. If not the line with cause an error.

68
Q

What’s the default value of a boolean?

A

false

69
Q

What’s the default value of a char?

A

‘\u0000’ (the unicode representation of the ASCII code for null)

70
Q

What’s the default value of a String?

A

null

71
Q

What are the naming restrictions on variable names?

A

Cannot contain whitespace
Cannot start with a digit
Cannot be a Java reserved word (Ex. data type names)
Needs to be unique within its scope (ex: only one declared with this name in a class or method)

72
Q

With Arrays if you don’t initialize it at the same time as the declaration what must the declaration statement contain?

A

new :
int[] numbers;
numbers = new int[5];
or
int[] numbers;
numbers = new int[]{1, 5, 6, 7};

73
Q

What is a use case?

A

A use case in programming refers to a description of how a user or an external system interacts with a software system to achieve a particular goal or task. It is a technique used in software development to identify, analyze, and document the requirements of a software system from a user’s perspective.

74
Q

What is composition?

A

Composition is a fundamental concept in object-oriented programming (OOP) where a class is made up of one or more objects of other classes. It allows creating complex types by combining simpler types, and it is used to build more complex objects by combining simpler objects.

In composition, the objects that are part of a class are called its “components” or “parts”, and they are typically stored as private member variables of the containing class. The containing class can then use the methods of its components to provide its own functionality. This allows for code reuse, as the same components can be used in different classes to provide similar functionality.

75
Q

does the java compiler require each public class to be it’s own file or in a shared file?

A

In Java, each public class must be defined in its own file with the name of the file matching the name of the class. However, you can have multiple non-public classes defined in the same file as the public class.

76
Q

The Java compiler requires that if a class is in a package, and if it uses any import statements, they must be in the order of:

A

Package declaration (if the class belongs to a package)
Import statements
Class declaration

77
Q

will .* import all the packages and child packages?

A

No, .* does not include the child packages

78
Q

What’s the method signature used to override the .equals method?

A

public boolean equals(Object o) {
}