Week 2 - Java foundations - Classes, Objects, Flow Control, Arrays Flashcards

1
Q

What is the purpose of the package declaration in a Java file?

A

The package declaration declares the package in which the class will reside. Packages are a way of organizing classes, interfaces, and enums in a hierarchical manner.

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

What is the naming convention for Java packages?

A

Packages follow a naming convention of lowercase characters separated by periods in the reverse way you would specify a web domain - thus, com.revature.mypackage instead of mypackage.revature.com.

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

What is a fully qualified class name in Java?

A

A fully qualified class name is the package declaration followed by the class name, in order to uniquely identify the class.

Let’s say we have a Java class named “Employee” that is located in the “com.company.hr” package. The fully qualified class name for this class would be “com.company.hr.Employee”.

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

What is the purpose of an import statement in Java?

A

An import statement is used to pull in other classes so that the programmer can just use the class name without the package.

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

What is the default package imported in Java?

A

By default, everything in the java.lang package is imported in Java, which gives us the System class.

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

What is a class declaration in Java?

A

A class declaration in Java declares a class with a specific name, and it is defined by wrapping it with curly braces.

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

What is the name of the method that serves as the entry point for Java applications?

A

The main method serves as the entry point for Java applications

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

What is the purpose of the main method in Java?

A

The main method is a special method that specifies the entry point of the application. It is invoked by the JVM when the code is executed.

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

What type of data does the main method return in Java?

A

The main method does not return any data in Java. The void keyword is used to denote this.

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

What is the purpose of the array of String objects passed to the main method in Java?

A

The array of Strings defined in the main method parameters are passed from the command line when the java command is run. They are used to pass input to the program at runtime.

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

What are the different scopes of a variable in Java?

A

The different scopes of a variable in Java are: instance, class, method, and block.

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

What is instance scope in Java?

A

Instance scope means that the variable is attached to individual objects created from the class. When an instance-scoped variable is modified, it has no effect on other, distinct objects of the same class.

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

What is class scope in Java?

A

Class scope means that the variable resides on the class definition itself. When objects update a class-scoped variable, the change is reflected across all instances of the class. Class scope is declared with the static keyword.

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

What is the method scope in Java?

A

Method scope is the scope of a variable declared within a method block, whether static or instance. Method-scoped variables are only available within the method they are declared and do not exist after the method finishes execution.

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

What is block scope in Java?

A

Block scoped variables only exist within the specific control flow block, of which there are several in Java: for, while, and do-while loops, if/else-if/else blocks, switch cases, or even just regular blocks of code declared via curly braces ({}).

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

What is the difference between a class and an object in Java?

A

A class is a template used to instantiate objects. An object is an instance of a class in memory.

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

What is a reference variable in Java?

A

A reference variable is a variable that stores the reference to an object in memory. It is used to interact with objects through their reference, which is the memory address used by the JVM to find a particular

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

Which is not a rule for which you can create a custom class in Java?

A

Syntax rules

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

What is TDD?

A

Test-driven development, or TDD, is a software development process that involves writing unit tests before implementing application code.

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

What is the TDD process?

A

The TDD process involves designing the interface/function, writing tests that use that function, refactoring method signatures, implementing functions, and running tests to verify functionality.

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

What is unit testing?

A

Unit testing is the testing of individual software components in isolation from the rest of the system, accomplished by writing tests that execute the code we want to inspect.

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

Why is TDD useful in software development?

A

Following the TDD process ensures that a valid unit test exists for any class or method written, and gives developers confidence in refactoring code without breaking existing functionality. When issues arise, the pinpointing of problems is easier with TDD.

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

What is the most common unit testing framework in Java?

A

JUnit is the most common unit testing framework in Java.

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

When following the Test Driven Development process (TDD), when should the tests be written?

A

Before any code is written

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

What is the testing of individual software components in isolation from the rest of the system?

A

Unit testing

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

A unit test should be run…?

A

Until the test passes

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

According to TDD, code should be rewritten…?

A

When a test case fails

28
Q

What are the benefits of using TDD in software development?

A

Benefits of using TDD include catching bugs early in the development process, ensuring that all code is tested for functionality, providing confidence in the codebase, and making debugging easier.

29
Q

Why is TDD important in software development?

A

TDD is important in software development because it ensures that most if not all code being written is tested for functionality. This can help catch bugs early on in the development process, leading to more reliable and robust code.

30
Q

JUnit Testing Approaches
In both the approaches the workflow remains common:

A

Creating a test case
Reviewing it
Rework if corrections needed
Execute the test case
Analyze the test results

31
Q

Automated Testing is preferred over manual Testing for the following reasons:

A

Manual testing requires a great deal of human effort.
Manual testing also requires more resources to be allocated.
Schedule constraints may put time allocated for testing at a premium.

32
Q

What Is JUnit?

A

JUnit is an open-source framework that is used for writing and executing unit tests in Java programming language. It is one of the best-known unit testing frameworks.

33
Q

Some crucial reasons for unit testing are listed below:

A

Unit testing helps testers and developers understand the root causes of errors.
Unit testing helps with documentation.
Unit testing fixes defects early in the development phase.
Unit testing helps with code reusability.

34
Q

Generally, the software goes through four levels of testing:

A

Unit Testing, Integration Testing, System Testing, and Acceptance Testing.

35
Q

In JUnit, what verifies that the state of the application meets what is expected?

A

Assertions

36
Q

In JUnit, what annotation declares a method as a test method?

A

@Test

37
Q

In JUnit, what annotation declares a setup method that runs once, before all other methods in the class?

A

@BeforeClass

38
Q

In JUnit, what annotation declares a setup method that runs before each test method?

A

@Before

39
Q

In JUnit, what annotation declares a tear-down method that runs before each test method?

A

@After

40
Q

A constructor declares how an object is to be instantiated and initialized from the class “blueprint”.

A

A constructor is a special method that initializes objects of a class.

It specifies how an object should be created and what initial values should be assigned to its attributes.

41
Q

A constructor is declared like a method, except

A

its method signature does not contain a return type, and a constructor always has the same name as the class

42
Q

The new object created by the constructor is always of the ________

A

class in which the constructor is declared.

43
Q

‘this’ refers to the object which is being instantiated. What are two ways to use this

A

it is used to initialize instance variables
to call other constructors (this is called constructor chaining)

44
Q

What does the ‘super’ keyword do?

A

references the “super”, or parent, class.

45
Q

When invoked as a method (super()), calls ______

A

the parent class constructor will be called

46
Q

A super() call (or a this() call) must ______

A

be the first line of any constructor

47
Q

The “default” constructor takes no arguments and simply calls super() (see above) - sometimes it is referred to as the ______

A

“default, no-args” constructor

48
Q

The if statement lets us execute a statement or a block of statements only if

A

some conditional test turns out to be true.

49
Q

The if statement depends on the use of

A

Boolean expressions.

50
Q

A Boolean expression is an expression that returns a

A

simple true or false result.

51
Q

A statement or block of statements is executed should the result of the Boolean expression be true
else and else if statements provide alternate routes for program execution to

A

follow should the result of the Boolean expression be false.

52
Q

switch statements attempt to

A

match some variable with the value it contains

53
Q

The following are the Java keywords used to create a variety of loops, depending on the looping situation required:
for : ____
while : ____
do-while : ____

A

for : This type of loop is used to execute a block of code a specific amount of times.
while : This type of loop evaluates a Boolean expression before executing the block of statements. Therefore, it may never execute.
do-while : This type of loop executes the block of statements one time then evaluates the Boolean expression at the end. Therefore, this type of loop executes at least once.

54
Q

The Java keyword break can be used to:

A

Exit a loop
Implement a “goto” flow control that exists in other programming languages
Terminate a switch statement sequence

55
Q

The Java keyword continue is used to

A

skip the current iteration of a loop.

56
Q

A break statement…

A

Can be used either with or without a label

57
Q

The main difference between a break and a continue statement is…

A

A continue directs program flow to the next loop iteration, a break immediately directs program flow out of the loop

58
Q

A continue statement Can be used either with or without a label
T/F?

A

True

59
Q

Which type of loop is most appropriate for executing a specific number of times?

A

for loop

60
Q

Which type of loop may not execute its code at all?

A

while loop

61
Q

Which type of loop will run its code at least once?

A

do-while loop

62
Q

Which type of loop will run its code at least once?

A

do-while loop

63
Q

Loops rely on what type of expression to tell it to stop looping?

A

Conditional

64
Q

Define an array.

A

An array is a contiguous block of memory storing a group of sequentially stored elements of the same type. Arrays in Java are of a fixed size and cannot be resized after declaration.

65
Q

What is an element

A

Each item of an array

66
Q

All elements of an array must be of the same

A

type

67
Q

3 ways to declare an array.

A

int[] arr;
int[] arr = {1, 2, 3, 4, 5};
int[] arr = new int[5];