Introduction To Java Flashcards

1
Q

What is a method?

A

A method is a block of code or collection of statements grouped together to perform a certain task or operation.

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

what are scope in Java?

A

The scope of a variable is the part of the program where the variable can be referenced/ used

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

what is Anonymous Array?

A

An array without a variable referencing it.
new int [] {1, 2 , 3, 4};

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

what are local variable?

A

A variable defined inside a method is called a local variable

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

what is a void method in Java?

A

A void method is a method that does not return a value

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

What is variable length Argument list?

A

A feature that allows a function to receive any number of arguments.

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

what are value returning method?

A

A method that return a value.
the return type is the type of the data that will be returned.

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

what is the return keyword in Java?

A

A keyword that can be used to return a value from a method.

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

name the types of method in Java?

A

the types of method in Java are :
1. predefined method method that is already defined in the Java class libraries .

  1. User-defined method the method written by the user or programmer .
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what is null in Java?

A

the value of an object that references nothing.
eg: String name; // null

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

what are Array?

A

A collection of variable of he same data type.

An array in java is an object.
An array variable references a group of data

the size of an array is fixed

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

what are parameter in Java?

A

parameter refers to the list of variables in a method declaration
parameter is a variable used to define a particular value during a function definition

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

what are arguments in Java?

A

Arguments are actual value passed into the method when the method is invoked or called

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

what are method overloading?

A

method overloading is writing the same method with different parameter.

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

what are Loops used for?

A

Loop are used to execute a block of code more than once.

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

what are the types of Loop in Java?

A

the types of loops in java are:
1. while loop
2. do-while loop
3. for loop
4. for each loop

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

what are while loop in java?

A

while Loop is a control flow statement that executes code repeatedly based on a given Boolean expression.

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

what is a infinite Loop?

A

A Loop which continues iterating indefinitely and never stops .

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

what is an IF Statement?

A

An IF Statement is used to execute a piece of code based on a condition

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

what is the Else Statement?

A

An optional block of an if statement

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

what is the break keyword?

A

exist the loop regardless of the condition.

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

what is the continue keyword?

A

skips the rest of the loop’s body Continue executing the loop as if we finished executing the body.

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

what are programming languages?

A

Programming languages are languages used to write computer program

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

what is a program?

A

Program is set of instruction written in specific programming languages that tell the computer what to do or how to complete a task.

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

what are machine language?

A

Machine language: A computer’s native language. Uses zeros & ones (0/1) the binary languages to add 2 and 3 and get the result.
Machine language is very hard to use. Machine dependent which means differs among different types of machines. Every instruction should be written in machine language before it can be executed.
All instruction written in other programming languages must be translated to machine code instructions.

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

what are assemble language?

A

Assembly language: was developed to make programming easier. Machine dependent.
Introduced keyboards/ add, sub,
A program called “assembler” translates assembly code to machine code.

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

what are high level language?

A

High level language: a new generation of programming languages. Uses English words/ easy to learn&use.
Machine independent, your program will run on different machines. Instructions are called “statements”. A program written in a high-level language is called a “source program or source code.”
A” compiler” or an “interpreter’’ is use to translate source code to machine code.

27
Q

what is a compiler?

A

A compiler translate all our source code into machine code at once

28
Q

Brief discussion about Java

A

The Java programming language is a high level programming language that is used to write varieties of program. Developed at sun Microsoft systems which were purchased by oracle in 2010.
Java is a general purpose and powerful programming language used for developing software that run on mobile, desktop, and servers. Java is machine independent.
Java was developed in 1995 which is modeled after c++.
Java was published in 1991
Java application fall into two categories: Applets and applications.

29
Q

what is an object?

A

An object of an instance of a class
An object is an entity having a specific identity, specific characteristics and specific behavior. Taking a car as an example of an object, it has characteristics like color, model, version, registration number, etc.it has behaviors like start the engine, stop the engine, accelerate the car, apply the brakes. Etc.

30
Q

what is a class?

A

A class is a blue print of an object
A class is a blue print that represents a set for objects that share common characteristic and behavior.

31
Q

what is a package?

A

A package is a container of a class which means a package host a class

32
Q

what is a keyword?

A

A keyword is a special word in your language, like: if or while. These words generally do not allowed to be an identifier; we call it a reserved word.

33
Q

what are identifiers?

A

Identifiers is the computer science word for a name of something you define in your code, like variable, constant, function, type, parameter, or similar thing.

34
Q

what is an expression?

A

An expression represents the computation of a value. Expressions can be numbers, identifiers (representing variables), function calls or can be made up of operators applied to other expressions. We can put parentheses around any expression. This will do the trick ( but spoiler alert: we will have to change it later)

35
Q

what are access modifiers?

A

Access modifiers are keyword that allow us to access classes ,method, and field
The types of access modifiers are :
1. Public: it is accessible in all classes in your application.
2. Protected: it is accessible within the class in which it is defined and in its subclass/es.
3. Private: It is accessible only within the class in which it is defined.
4. Default: It is declared/defined without using any modifier. It is accessible within the same class and package within which its class is defined.

36
Q

What are relational operator?

A

Java Relational Operators are a bunch of binary operators used to check for relations between two operands, including equality, greater than, less than, etc. They return a boolean result after the comparison and are extensively used in looping statements as well as conditional if-else statements and so on.

37
Q

Name and discuss the types of naming conventions in Java.

A

The types of naming conventions in Java are:
1. Camel case naming convention: camel case is used when naming variable and methods in Java.
The first word/letter is not capitalized after that all letter are capitalized at the beginning of each word.
Example: thisIsAName
2. Snake case naming convention: this is used when naming constant in Java
Example: This_Is_A_Name
3. Pascal case naming convention: capitalized the beginning of each word.
Used with classes example: ThisIsAName.

38
Q

name the type of Data type used in Java

A

The data type used in Java are:
1. Text(string)
2. Boolean ( true/false)
3. Number(232)
4. User-defined data type

39
Q

what are condition?

A

Conditions are used to control what to execute in our program

40
Q

what is case sensitivity in Java?

A

Java is case sensitive which mean that Java distinguishes between the upper case letter and the lower case letters.
Example: Java, java, JAVA, is not the same thing in java

41
Q

what is Unicode encoding scheme in Java?

A

Unicode encoding scheme : computers see characters as binary numbers (0/1)
Each character is encoded then stored in memory.
Java uses the Unicode encoding scheme:
o Character to Unicode to binary.
o Binary to Unicode to character.

42
Q

what is encoding?

A

Encoding is mapping each character to its binary representation with the help of an encoding scheme.

43
Q

what is platform independent in Java?

A

Is a program capability of moving easily from one computer system to another computer system

44
Q

what is the difference between a while loop and a do while loop?

A

The difference between a while loop and a do-while loop is that a while loop check the condition and then execute the statements while a do-while loop execute at least one time before checking the conditions.

45
Q

What is Reference variable?

A

A variable that point to an object of a given class.
Or reference to the address of another variable.

46
Q

What are primitive variable?

A

A primitive variable information is stored as the value of that variable

47
Q

What is dynamic memory Allocation?

A

Means that during runtime memory is allocated.

48
Q

What is conditional Operator?

A

An operator that evaluate an expression based on a condition.

SYNTAX:
boolean-expression? expression 1: expression 2

49
Q

Array Explained

A

What is an Array?
An array is a collection of similar data elements stored at contiguous memory locations. It is the simplest data structure where each data element can be accessed directly by only using its index number.

For instance, if we want to store the marks scored by a student in 5 subjects, then there’s no need to define individual variables for each subject. Rather, we can define an array that will store the data elements at contiguous memory locations.

Array marks[5] define the marks scored by a student in 5 different subjects where each subject’s marks are located at a particular location in the array, i.e., marks[0] denote the marks scored in the first subject, marks[1] denotes the marks scored in 2nd subject and so on.

Need of using Array –
In programming, most of the cases need to store a large amount of data of a similar type. We need to define numerous variables to store such a huge amount of data. While writing the programs, it would be very tough to memorize all variable names. Instead, it is better to define an array and store all the elements in it.

The following example illustrates how an array can be used while writing a code-

In the following example, we have given marks to a student in 5 different subjects. The aim is to calculate the average of a student’s marks.

Without Using Array
1
2
3
4
5
6
7
#include<stdio.h>
void main()
{
int subject1 = 40, subject2 = 52, subject3 = 47, subject4 = 70, subject5= 67;
float avg = (subject1 + subject2 + subject3 + subject4 + subject5)/5;
printf(avg);
}
Using Array
1
2
3
4
5
6
7
#include<stdio.h>
void main()
{
int subject1 = 40, subject2 = 52, subject3 = 47, subject4 = 70, subject5= 67;
float avg = (subject1 + subject2 + subject3 + subject4 + subject5)/5;
printf(avg);</stdio.h></stdio.h>

50
Q

What is concatenation

A

Concatenation is the process of combining two or more strings to form a new string by subsequently appending the next string to the end of the previous strings.

51
Q

What are logical operator?

A

AND Operator ( && ) – if( a && b ) [if true execute else don’t]
OR Operator ( || ) – if( a || b) [if one of them is true to execute else don’t]
NOT Operator ( ! ) – !(a<b) [returns false if a is smaller than b]

52
Q

The types of Logical Operator?

A

. Logical ‘AND’ Operator (&&)
This operator returns true when both the conditions under consideration are satisfied or are true. If even one of the two yields false, the operator results false. In Simple terms, cond1 && cond2 returns true when both cond1 and cond2 are true (i.e. non-zero).

Syntax:

condition1 && condition2

2.Logical ‘OR’ Operator (||)
This operator returns true when one of the two conditions under consideration is satisfied or is true. If even one of the two yields true, the operator results true. To make the result false, both the constraints need to return false.

Syntax:

condition1 || condition2

3.Logical ‘NOT’ Operator (!)
Unlike the previous two, this is a unary operator and returns true when the condition under consideration is not satisfied or is a false condition. Basically, if the condition is false, the operation returns true and when the condition is true, the operation returns false.

Syntax:

!(condition)

53
Q

The Break keyword

A

Syntax:

break keyword along with a semicolon

break;
Usage of Break keyword in Java
Break keyword is often used inside loops control structures and switch statements. It is used to terminate loops and switch statements in java. When the break keyword is encountered within a loop, the loop is immediately terminated and the program control goes to the next statement following the loop. When the break keyword is used in a nested loop, only the inner loop will get terminated. Even it is used with if statement to terminated when a certain condition is met.

The break keyword has special usage inside the switch statement. Every case in the switch statement is followed by a break keyword, such that whenever the program control jumps to the next case, it wouldn’t execute subsequent cases.

54
Q

The continue Keyword

A

Definition and Usage
The continue keyword is used to end the current iteration in a for loop (or a while loop), and continues to the next iteration.

55
Q

Difference between Break and Continue

A

continue” in Java means go to end of the current loop, means: if the compiler sees continue in a loop it will go to the next iteration

Example: This is a code to print the odd numbers from 1 to 10

the compiler will ignore the print code whenever it sees continue moving into the next iteration

for (int i = 0; i < 10; i++) {
if (i%2 == 0) continue;
System.out.println(i+””);
}

Continue is a keyword in Java & it is used to skip the current iteration

continue statement in the above program simply skips the iteration when i is even and prints the value of i when it is odd.

Continue statement simply takes you out of the loop without executing the remaining statements inside the loop and triggers the next iteration

While Break keyword
Break keyword is often used inside loops control structures and switch statements. It is used to terminate loops and switch statements in java. When the break keyword is encountered within a loop, the loop is immediately terminated and the program control goes to the next statement following the loop. When the break keyword is used in a nested loop, only the inner loop will get terminated. Even it is used with if statement to terminated when a certain condition is met.

The break keyword has special usage inside the switch statement. Every case in the switch statement is followed by a break keyword, such that whenever the program control jumps to the next case, it wouldn’t execute subsequent cases.
show how to use the break keyword within For loop. Considering when the value of ‘i’ becomes 39, the break keyword plays its role and terminate the for a loop. All values of ‘i’ before 39, are displayed in the result.

// Java Program to show use of break statement
// inside the For loop
public class GFG {

// Main driver code
public static void main(String[] args)
{
    // For loop for iteration
    for (int i = 3; i <= 50; i += 3) {
        if (i == 39) {
            // Using Break keyword to suspend
            // loop when i become 39
            break;
        }
 
        // Printing elements showcasing break usage
        System.out.print(i + " ");
    }
}
56
Q

How can we fill an Array?

A

Int[] numbers = new int[8] {0,0,0,0,0,0,0,0}

Arrays.fill(number, 3); // {3,3,3,3,3,3,3,3,}

57
Q

How do we print Array?

A

There are two ways to prints Array:
1.int[] numbers = {1,2,3,4,5,6,7}
for ( int i=0; i < number.length; i++)
System.out.println( number[i] + “ “ “);

58
Q

Naming Convention in Java

A
  1. Java variable naming conventions
    For variables, the Java naming convention is to always start with a lowercase letter and then capitalize the first letter of every subsequent word. Variables in Java are not allowed to contain white space, so variables made from compound words are to be written with a lower camel case syntax.

Here are three examples of variables that follow the standard Java naming convention:

firstName
timeToFirstLoad
index
2.

59
Q

Naming convention in JAVA explained

A

Log In
Join
Trusted answers to developer questions
What are the naming conventions in Java?
Grokking the Behavioral Interview

Many candidates are rejected or down-leveled in technical interviews due to poor performance in behavioral or cultural fit interviews. Ace your interviews with this free course, where you will practice confidently tackling behavioral interview questions.

Get Free Course
Overview
Naming conventions in Java are a set of rules that apply to Java programs, and are defined in The Java Language Specification. They include recommendations for naming packages, classes, methods, and variables.

This shot discusses some basic naming conventions in Java.

PascalCasing
PascalCasing is a naming convention where each word in a compound word is capitalized. “JavaNamingConventions” is an example of a PascalCased word in Java.

camelCasing
In camelCasing, the first word in a compound word is not capitalized. “javaNamingConventions” is an example of a camelCased word in Java.

snake_casing
In snake_casing, underscores separate the words in a compound word. “java_naming_conventions” is an example of a snake_cased word in Java.

Naming conventions in Java
Construct

Convention

Description

Examples

Class

PascalCase

Classes should be named using PascalCasing, with the first letter of each word capitalized. A class name should be a noun or a word representative of an object.

MyClass, Employee

Interface

PascalCase

Interfaces should be named using PascalCasing, with the first letter of each word capitalized. An interface name should be an adjective.

Serializable, Runnable

Method

camelCase

Method should be named using camelCasing. Method names should always start with a lower case letter.

main(), print(), println() etc.

Variable

camelCase

Variables should be named using camelCasing. Variable names should always start with a lower case letter.

num1, name, address

Package

snake_case (all lower case)

Packages should be named using snake_casing. Package names should always start with a lower case letter.

java, lang, util

Constant

snake_case

(ALL UPPER CASE)

Constant variables should be named using snake_casing. Contant variable names should have all upper case letters.

MAX_VALUE, MIN_VALUE etc.

Code example
Below is a code example demonstrating the usage of Java naming conventions.

package io.educative.example;
public class Employee{
public static final int MAX_AGE=100; //Constant variable naming convention
private String empName; //Variable naming convention
private int age;
public Employee(String name, int age){ //Constructor naming convention
this.empName=name;
this.age=age;
}
public void setName(String name){ //Method naming convention
this.empName = name;
}
public int getAge(){ //Method naming convention
return age;
}
public static void main(String args){ //Main method naming convention
Employee emp1 = new Employee(“John Doe”, 30);
System.out.println(“Employee Name: “+emp1.empName); //Prints Employee Name
System.out.println(“Employee Age: “+emp1.getAge()); //Prints Employee Age
}
}
Java code demonstrating naming conventions
Explanation
In the above code example, we follow different naming conventions for different parts of the code. We use PascalCasing for class and interface names, camelCasing for method and variable names, and snake_casing for constant variable names. We also use lower case letters for package names

60
Q

What is Execution in programming?

A

A program in running state

61
Q

When to use break and continue in Java

A

The break and continue statements are the jump statements that are used to skip some statements inside the loop or terminate the loop immediately without checking the test expression. These statements can be used inside any loops such as for, while, do-while loop.

Break: The break statement in java is used to terminate from the loop immediately. When a break statement is encountered inside a loop, the loop iteration stops there, and control returns from the loop immediately to the first statement after the loop. Basically, break statements are used in situations when we are not sure about the actual number of iteration for the loop, or we want to terminate the loop based on some condition.

Syntax :

break;
In Java, a break statement is majorly used for:

To exit a loop.
Used as a “civilized” form of goto.
Terminate a sequence in a switch statement.
Using break to exit a loop

Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop. When we use break inside the nested loops, it will only break out of the innermost loop.

Example:

Java
// Java program to demonstrate using
// break to exit a loop
class GFG {
public static void main(String[] args)
{
// Initially loop is set to run from 0-9
for (int i = 0; i < 10; i++) {
// Terminate the loop when i is 5
if (i == 5)
break;
System.out.println(“i: “ + i);
}
System.out.println(“Out of Loop”);
}
}
Output
i: 0
i: 1
i: 2
i: 3
i: 4
Out of Loop
Using break as a Form of Goto

Java does not have a goto statement because it provides a way to branch in an arbitrary and unstructured manner. Java uses a label. A Label is used to identify a block of code.

Syntax:

label:
{
statement1;
statement2;
statement3;
.
.
}
Now, the break statements can be used to jump out of the target block. We cannot break to any label which is not defined for an enclosing block.

Syntax:

break label;
Example:

Java
// Java program to demonstrates using break with goto
class GFG {
public static void main(String args[])
{
// First label
first:
for (int i = 0; i < 3; i++) {
// Second label
second:
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {

                // Using break statement with label
                break first;
            }
            System.out.println(i + " " + j);
        }
    }
} } Output 0 0 0 1 0 2 1 0 Using break to terminate a sequence in a switch statement.

The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression. The break statement is used inside the switch to terminate a statement sequence. The break statement is optional. If omitted, execution will continue on into the next case.

Syntax:

switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}
Example:

Java
// Java program to demonstrate using break to terminate a
// sequence in a switch statement.
class GFG {
public static void main(String args[])
{
int i = 2;
switch (i) {
case 0:
System.out.println(“i is zero.”);
break;
case 1:
System.out.println(“i is one.”);
break;
case 2:
System.out.println(“i is two.”);
break;
default:
System.out.println(“Invalid number”);
}
}
}
Output
i is two.
Continue:

The continue statement in Java is used to skip the current iteration of a loop. We can use continue statement inside any types of loops such as for, while, and do-while loop. Basically continue statements are used in the situations when we want to continue the loop but do not want the remaining statement after the continue statement.

Syntax:

continue;
Using continue to continue a loop

Using continue, we can skip the current iteration of a loop and jumps to the next iteration of the loop immediately.

Example:

Java
// Java program to demonstrates the continue
// statement to continue a loop
class GFG {
public static void main(String args[])
{
for (int i = 0; i < 10; i++) {
// If the number is 2
// skip and continue
if (i == 2)
continue;

        System.out.print(i + " ");
    }
} } Output 0 1 3 4 5 6 7 8 9 Using continue as a labelled continue statement

The unlabeled continue statement is used to continue the innermost loop. However, since JDK 1.5 java introduces another feature known as labelled continue statement. We can use a labelled continue statement to continue the outermost loop.

Example:

Java
// Java program to demonstrates labeled continue statement
class GFG {
public static void main(String args[])
{
// First label
first:
for (int i = 0; i < 3; i++) {
// Second label
second:
for (int j = 0; j < 3; j++) {
if (i == 1 && j == 1) {

                // Using continue statement with label
                continue first;
            }
            System.out.println(i + " " + j);
        }
    }
} } Output 0 0 0 1 0 2 1 0 2 0 2 1 2 2
62
Q

Difference between break and continue in Java

A

Difference between break and continue:

Break

Continue

The break statement is used to terminate the loop immediately.

The continue statement is used to skip the current iteration of the loop.

break keyword is used to indicate break statements in java programming.

continue keyword is used to indicate continue statement in java programming.

We can use a break with the switch statement.

We can not use a continue with the switch statement.

The break statement terminates the whole loop early.

The continue statement brings the next iteration early.

It stops the execution of the loop.

It does not stop the execut

63
Q

Iteration means what?

A

Iteration is the process of repeating steps

Iteration in programming means executing a sequence of code instruction until a specific condition is true
Iteration in programming means repeating steps, or instructions, over and over again. This is often called a ‘loop’.

64
Q

What DRY means in programming

A

Don’t Repeat yourself

65
Q

What is Redundancy?

A

Code that are in your source code, but are not required or have no logical effect to the output.

66
Q

What is the meaning of typo in programming?

A

A typo is a spelling error that can be fixed.