WEEK 7 PART 1 Flashcards

1
Q

What is OOP?

A

OOP is a design concept. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old programming languages (C, Pascal, etc.). Everything in OOP is grouped as self- sustainable “objects”. In OOP we use different programming languages like - Ruby, Python, Java, C++ etc. Simula was the first OOP language.

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

What is a Class?

A

A class is simply a representation of a type of object. It is the blueprint, or plan, or template, that describes the details of an object. A class is the blueprint from which the individual object is created. Class is composed of three things: a name, attributes, and operations.

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

What is an Object?

A

Object is an instance of a class. An object in OOPS is nothing but a self-contained component that consists of methods and properties to make a particular type of data useful.

Objects are derived from the Class by the blueprint or design of a class. For example: if we have a Student class, we can derive objects from it as follows -

Student objStudent = new Student;

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

What are the 4 principles of OOP?

A

Object-oriented programming has four basic concepts:

  • *1.Encapsulation**
  • *2.Abstraction**
  • *3.Inheritance**
  • *4.Polymorphism**
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What Is Encapsulation?

A

Encapsulation is one of the best Java OOPs concepts of wrapping the data and code. In this OOP concept, the variables of a class are always hidden from other classes. It means data hiding. This will wrap the code and data into a single unit and be better for ‘unit testing’. It can only be accessed using the methods of their current class. For example – in school, a student cannot exist without a class.

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

What Is Abstraction?

A

Abstraction is an act of representing essential features without including background details. It is a technique of creating a new data type that is suited for a specific application. For example, while driving a car, you do not have to be concerned with its internal working. Here you just need to be concerned about parts like the steering wheel, Gear, accelerator, etc.

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

What is Inheritance?

A

Inheritance is one of the Basic Concepts of OOPs in which one object acquires the properties and behaviors of the parent object. It is basically creating a parent-child relationship between two classes. Child class can obtain all the features from a parent class and also allows us to reuse the methods of that class.

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

What Is Polymorphism?

A

Polymorphism refers to one of the OOPs concepts in Java which is the ability of a variable, object, or function to take on multiple forms. Polymorphism happens when multiple classes are related to each other due to inheritance.

For example, in English, the verb run has a different meaning if you use it with a laptop, a foot race, and business. Here, we understand the meaning of run based on the other words used along with it. The same also applied to Polymorphism.

It’s used in Method Overloading and Method Overriding.

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

Data types in Java

A

Java has two categories of data:

  • *1. Primitive Data Type:** These data types are pre-defined and can’t be assigned a null value as these have their own value. For example - boolean, char, int, short, byte, long, float, and double
  • *2. Non-Primitive Data Type or Object Data type:** These data types are created by the programmers, are not pre-defined and can be assigned with null values. For example - String, Array, etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Primitive Datatypes

A

A primitive data type is pre-defined by the programming language. The size and type of variable values are specified, and it has no additional methods.

1. boolean: A boolean data type comprises of a bit of information and can store only true or false values. This data type is used to track true/false conditions.

2. byte: This is an example of a primitive data type. It is an 8-bit signed two’s complement integer. It stores whole numbers that lie between -128 to 127. A byte data type is helpful for saving memory in large amounts.

3. short: A short data type is greater than byte in terms of size and less than a integer. It stores the value that ranges from -32,768 to 32767. The default size of this data type: 2 bytes.

4. int: This data type can store whole numbers from -2147483648 to 2147483647. Generally, int is the preferred data type when you create variables with a numeric value.

5. long: This data type is a 64-bit two’s complement integer. By default, the size of a long data type is 64 bit and its value ranges from -263 to 263-1.

6. float: You should use a floating point type whenever you need a number with a decimal, such as 8.88 or 3.14515.

7. double: The double data type can store fractional numbers from 1.7e−308 to 1.7e+308. Note that you should end the value with a “d”:

8. char: This data type is used to store a single character. The character must be enclosed within single quotes, like ‘E’ or ‘e’. Alternatively, you can also use ASCII values to display certain characters.

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

Non-Primitive Datatypes

A

Non-Primitive data types refer to objects and hence they are called reference types. Examples of non-primitive types include Strings, Arrays, Classes, Interfaces, etc.

1. Strings: String is a sequence of characters. But in Java, a string is an object that represents a sequence of characters. The java.lang.String class is used to create a string object.

2. Arrays: Arrays in Java are homogeneous data structures implemented in Java as objects. Arrays store one or more values of a specific data type and provide indexed access to store the same. A specific element in an array is accessed by its index.

3. Classes: A class in Java is a blueprint that includes all your data. A class contains fields(variables) and methods to describe the behavior of an object.

4. Interface: Like a class, an interface can have methods and variables, but the methods declared in the interface are by default abstract (only method signature, nobody).

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

Difference between primitive and non-primitive data types

A

Primitive types are predefined in Java. Non-primitive types are created by the programmer and are not defined by Java.
Non Primitive types can be used to call methods to perform certain operations, while primitive types cannot.
A primitive type always has a value, whereas non-primitive types can be null.
A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.
The size of a primitive type depends on the data type, while non-primitive types have all the same size.

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

What is a method/function?

A

When a few lines of code are grouped together is called a method. The benefits of creating method are to create
cleaner code, increase reusability, and easy maintenance.

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

Type of method

A

there are two types of method
1. void - this type of method does not return any value

2. Non-void - this type of method does always return value based on the method’s data type

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

Can we declare the main method of our class as private?

A

In java, the main method must be public static in order to run any application correctly. If the main method is declared as private, the developer won’t get any compilation error however, it will not get executed and will give a runtime error.

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

What is a Variable?

A

● Variables are used to store information to be referenced and manipulated in a computer program.
● They also provide a way of labeling data with a descriptive name, so our programs can be
understood more clearly by the reader and ourselves.
● It is helpful to think of variables as containers that hold information. Their sole purpose is to label
and store data in memory.
● This data can then be used throughout your program

17
Q

Variables Scope

A
  • *Local scope:** Only available within the method or within the loop.
  • *Global scope**: Available to all the methods and loops.
18
Q

Types of variables

A

In Java, there are three types of variables:

1) Local Variables
Local Variables are a variable that is declared inside the body of a method.

2) Instance Variables
Instance variables are defined without the STATIC keyword . They are defined Outside a method declaration. They are Object-specific and are known as instance variables.

3) Static Variables
Static variables are initialized only once, at the start of the program execution. These variables should be initialized first, before the initialization of any instance variables.

19
Q

What are method parameters and method arguments?

A

1. Method Parameter: A parameter is a variable used to define a particular value during a function definition. Whenever we define a function we introduce our compiler with some variables that are being used in the running of that function. These variables are often termed as Parameters. In short, the parameter is a variable that is defined during the declaration of the method.

2. Method Argument: An argument is a value passed to a function when the function is called. Whenever any function is called during the execution of the program there are some values passed with the function. These values are called arguments. In short, the value we pass for the parameters in the equation or functions is called arguments.

20
Q

What are the various access specifiers/modifiers for Java classes?

A

In Java, access specifiers are the keywords used before a class name that defines the access scope. Access modifiers basically dictate the access of a class, method, constraints to another class. The types of access specifiers for classes are:

1. Public: Class, Method, Field is accessible from anywhere. These are assigned on the Package level, all classes and methods from the same class will have access to this.

2. Protected: Method, Field can be accessed from the same class to which they belong or from the sub-classes, and from the class of the same package, but not from outside.

3. Default: Method, Field, the class can be accessed only from the same package and not from outside of its native package. These are also assigned on the Package level, however, this is mostly used in the parent-child relationship.

4. Private: Method, Field can be accessed from the same class to which they belong.

21
Q

What is the difference between double and float variables in Java?

A

In java, float takes 4 bytes in memory while Double takes 8 bytes in memory. Float has a precision of 6 decimal places, while Double Has a precision of 15 decimal places.

22
Q

What are Loops in Java? What are the three types of loops?

A

Looping is used in programming to execute a statement or a block of statement repeatedly. There are three types of loops in Java:

1) For Loops
For loops are used in java to execute statements repeatedly for a given number of times. For loops are used when the number of times to execute the statements is known to the programmer. It is a repetition management system,

2) While Loops
While loop is used when certain statements need to be executed repeatedly as long as a condition is fulfilled. In while loops, the condition is checked first before the execution of statements.

3) Do While Loops
Do While Loop is the same as While loop with the only difference is that condition is checked after execution of a block of statements. Hence in the case of the do-while loop, statements are executed at least once.

23
Q

Conditional Statements

A

If Statement: In Java conditional statements is used by the following diagram. So if the condition is true then perform conditional code and if the condition is false do something else

If Else Statement: An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Nested If Statement: It is always legal to nest if-else statements which means you can use one if or else if statement inside another if or else if statement

Switch Statement: A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

24
Q

What’s ‘Stale’ in Java?

A

Stale state in information in an object that doesn’t reflect the reality or underlyingpersistent value. Basically when an object’s stored view or cached view of the system b3qcies outdated. Eg: an object’s members are filled with information from a database but the underlying data in the database has changed since the object was filled.