Core Java Flashcards

1
Q

Learning Objectives
To install the JDK using Synaptic
Package Manager.

A

To run java program, we need to install JDK and Java development kit

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

Why Java?

A
Simple
Object oriented
Platform independent
Safe
High performance
Multi-threaded
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Types and applications of Java.

A
Types and Applications of Java
JSP or Java Server Pages
Java Applets
J2EE or Java Enterprise Edition
JavaBeans
Mobile Java
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create a simple java program.

A
class HelloJava {
    public static void main(String[] args){
    System.out.println("My first Java program");
}
}
Description :-
Public - access modifier, visible to all
Static - no need to create object to invoke static method
Void - return type.
Main - starting point
System.out.println - print statement
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Naming Conventions in Java

A
The class name should be in CamelName
Example:- PayPal, iPhone, MasterCard.

The method name should be in mixed case
Also method name should be an verb
Example :- showString(), main(),
goToHelp().

Variable name should not begin with digits

We cannot use keywords for class, method and variable name
Example:- Cannot use keywords like
public, private, void, static and many
more.

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

Install Eclipse on Ubuntu

Install Eclipse on Redhat

A

fetch software update
-> sudo apt-get update

install software
-> sudo apt-get install eclipse
Ubuntu - Similar OS
Debian
Kubuntu & Xubuntu

fetch software list
->sudo yum upate

install software
->sudo yum install eclipse

Redhat - Similar OS
Fedora
Centos
SUSE Linux

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

—-Introduction to Eclipse—-

What is Eclipse?

A

Eclipse is a Integrated development environment. One can write, debug and run Ava program with ease

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

Possible Errors

A
Missing semicolon
 Missing double quotes
Mis-match file name and class name
Typing print statement in lower case
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Programming Features Used in

Eclipse IDE

A

Auto completion
Syntax error highlighting
Error dialog box
Shortcut keys

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

Data types in Java

A

There are in total 8 data types defined in Java programming known as the primitive type

Numerical data type
int, float

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

Arithmetic Operations

A
Addition : +
Subtraction : -
Multiplication : *
Division : /
Modulo Operation(Remainder after division) : %
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
String
Create strings
Add strings
Convert to lower case
Convert to upper case
A

String in Java, is a sequence of
characters

public class StringDemo {
    public static void main(String agrs[]){
        String greet = "HeLlO";
        String name = "JaVa";
        char SPACE = ' ';
        greet = greet.toLowerCase();
        name = name.toUpperCase();
        String msg = greet + SPACE + name;
        System.out.println(msg);
    }
}

Output :-
hello JAVA

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

Type Conversion

A

Converting one data type to another

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

Type Conversion

A

Converting one data type to another

Java facilitates type conversion in two forms:
Implicit (Automatic) Type Conversion
Explicit Type Conversion

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

Implicit type conversion

A

Also called as widening conversion because the compiler converts the value of (smaller size) type to the boarder(large size)

byte ——— is convertible to ————-> short, int, long, float, or double
Short ——— is convertible to ————-> int, long, float, or double
char ——— is convertible to ————-> int, long, float, or double
int ——— is convertible to ————-> long, float, or double
long ——— is convertible to ————-> float or double
float ——— is convertible to ————-> double

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

Explicit type conversion

A

It is also called as narrowing conversion(narrowing of broader data type to smaller data type). The type casting is done manually by the programmer and not by the compiler

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q
Integer toString(int i)
Float toString() Method
A
Java toString() method is a part of the Integer class of the java.lang package
used to convert integer value into String.

Syntax:-
Integer.toString(a)

Java toString() method is a part of the float class if the java.lang package
used to convert float value to string

Syntax
a.toString()

18
Q

Relational operators

A

Used to check the condition
The size pf boolean data type is 1 bit
It stores only two values(true/false)

19
Q

Logical operators

A

They are used to check multiple conditions

and &&
or ||
not !

20
Q

Conditional Statements

A

Control the flow of execution of the program

Types:
If : single conditional statement. Executes a block of statements

If-else : Single condition. Executes alternatives statements

if else if : Multiple conditions. Executes various sets of statements. Called as branching or decision making statement

nested if : An if statement inside another if statement

switch

21
Q

Rules for conditional statement, ternary operator

A

Add a semicolon for terminating the sentence
Do not add colon after the condition
Add block of code within curly braces

ternary operator : conditional operator providing similar results as if else statement. Short syntax and denoted by ?. Takes three operands at a time.

Syntax :-
Result = Expression ? operand 1 : operand 2

22
Q

Arrays

A

Marks: 15, 12, 13, 11, 12, 10
Names: Arun, Bala, David
Temperatures: 33.2, 31.7. 32.3
Rainfall: 25, 31, 29, 13, 27, 3

An array is a collection of similar data types

23
Q

Array Operations
Arrays.toString
copyOfRange() method

A

import java.utiil.Arrays;

Arrays.toString
Converts any datatype of of array to string
–> Arrays.toString(var_name)

copyOfRange() method
This method ensures that the continuity of of ranges is maintained. It receives a copy of array from starting index and the ending index of the array

(0,4) implies from index 0 to 3

int[] array = {1,2,3,4,5,6,7};
int[] array1 = Arrays.copyOfRange(array , 3,5);
for(int num : array1)
System.out.println(num);

–> 4,5

24
Q
Array sort()
Array fill()
Array copyOf()
Arrays.equals()
A
Array sort() method
Sort  method of array class. It accepts array and sorts them in ascending order. We can extend this feature by providing start_index and end_index

Arrays.sort(arr);

Array fill() method
fill() method of array class. We can assign a particular value to all array elements or in a specified range

Arrays.fill(arr, 5);

Array copyOf()
Similar to copyOfRange() method

Arrays.copyOf(arr, 4);

Arrays.equals() array
Compare two array by comparing each elements of the array.

25
Q
A class in real world
A class in Java
Structure of a Java Class
A
  1. Whatever we can see in the world are all objects
    All the object can be categorized into special groups
    Each group is termed as class

Example :- class of human being
objects :- different people
Properties :- Eyes, hands, legs
Behaviors: seeing, eating, walking

  1. Class is a blueprint, from which objects are created
  2. Set of properties :- variables
    set of behaviors :- methods
26
Q

Members of a class
What is object
Reference Variables

A

Methods
Fields/Variables

2. Object is instance of class
It stores its state in fields/variables
It exposes its behavior through methods
  1. Variables that refer to objects is called as reference variables
27
Q

Instance Fields

A

Objects store their individual state in instance fields
They are declared without the static keyword
Non static fields are also known as instance variables or instance fields
Accessing the fields using dot operator

Instance variables are variables that are declare inside a class but outside any method,
class Student
{
    String name;
    int age;
}
28
Q

Java method

A

It is a collection of statements that performs a specific task

29
Q

Java constructor

Difference between constructor and method

A
Constructor is used to initialize instance variables. It is called creation of object. Constructor has the same name as the class name
Example:-
class Test{
	Test(){
	//constructor of the body
	}
}

Constructor - no return type
Method - return type

Constructor - it is called using new operator
Method is called using dot operator

30
Q

Parameterized constructor

A

A constructor that has parameter is called parametrized constructor. It can have more than one parameter

When we call a parameterized constructor, values are passed to it, they are known as arguments

31
Q

this keyword

A

Within the constructor, this is the reference to the current object.
It helps to avoid name conflicts between local variables and instance variables
This is called explicit constructor
invocation.

32
Q

Non static keyword

When is a non-static block executed?

A

Any code written between two curly brackets is a non-static block.

When is a non-static block executed?
Non-static keyword is executed for each object  that is created
It executed before the constructor execution
It can initialize instance members of the class
Any other execution like calculation could also be given in the block
33
Q

What is Constructor Overloading?

A

Define multiple constructors for a class.

They must differ in numbers or types of parameters.

34
Q

What is method overloading

A

Define two or more methods with same name within a class.
They must differ in number or types of parameters.
These methods are called overloaded methods.
The process is called method overloading.

35
Q

What is bufferReader?

How to make use of BufferedReader?

A

Class in Java which is used to read text from input stream
Provides efficient way to read characters, array of characters and lines

Import three classes

  1. IO exception
  2. InputStreamReader
  3. BufferedReader
Syntax:-
Syntax
I InputStreamReader isr=new
InputStreamReader(System.in);
BufferedReader br= new
BufferedReader(isr);
36
Q

Subclassing

Private member in subclass

A
It is a way to create a new class from existing class.
The new class created is subclass, child class or derived class
The already existing class is called super class or base class or parent class 
Subclass cannot directly access the private members of the superclass
37
Q

Superclass

A

It can have protected or private methods
These methods can access private methods
The subclass can also access private fields through these methods

38
Q

Method overriding

Annotations

A

A method in the subclassis said to override the method in the parent class if: name
return type
argument list
matches exactly

Annotations
start with at sign character(@)
provide data about a program
have no direct effect on the operation
of the code
Override Annotation
If a method is annotated with @Override,
compiler generates error if:
the method does not override a
method declared in its superclass
the method signature is different in its
superclass
39
Q

Super keyword

A

A subclass can use a superclass data or method using the super keyword

The super keyword:
refers to the instance variable of the parent class
is used to invoke parent class constructor
is used to invoke parent class method
40
Q

What is final keyword?

A

final is a keyword or reserved word in Java
It can be applied to variables, methods or classes

final variable is
a variable whose value cannot be changed
It will be a constant

final variable as static
static final variables cannot be initialized in a constructor.
They must be assigned a value with their declaration
They must be declared in a static block
Static variables are shared among all the objects of a class
Creating a new object would change the static variable
This is not allowed if the static variable is final
final and private method
private methods are not inherited by the child class
So we can add a method getDetails() in the child class
41
Q

What is Polymorphism?

A

Polymorphism is an ability of an object to take on many forms

Advantages of Polymorphism
Reduction of complexity
Code re-usability

Types of Polymorphism
Compile-time or Method Overloading or Static Binding
Run-time polymorphism or Method Overriding or Dynamic Binding