Java Questions

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

What are the default values for local variables

The local variables are not initialized to any default value, neither primitives nor object references

notify vs notifyAll

The notify() method is used for sending a signal to wake up a single thread in the waiting pool. Contrarily, the notifyAll() method is used for sending a signal to wake up all threads in a waiting pool.

Association

The relationship between two objects, where each object has its separate lifecycle. There is no ownership

Rules for public modifier in java

Yes: all

Polymorphism

refers to the ability of a method, object, or variable to assume several forms

Advantages of packages in java

- Classes of other programs can be reused. - Two classes with the same name can exist in two different packages. - Packages can hide classes, thus denying access to certain programs and classes meant for internal use only. - They also separate design from coding.

4 Main Features of Java

- High Performance- Using a JIT (Just-In-Time) compiler allows high performance in Java. The JIT compiler converts the Java bytecode into machine language code, which then gets executed by the JVM. - Multi-threading- A thread is a flow of execution. The JVM creates a thread which is called the main thread. Java allows the creation of several threads using either extending the thread class or implementing the Runnable interface. - OOPS Concepts- Java follows various OOPS concepts, namely abstraction, encapsulation, inheritance, object-oriented, and polymorphism - Platform Independency- Java makes use of the Java Virtual Machine or JVM, which allows a single Java program to operate on multiple platforms without any modifications.

What is the thread lifecycle in java

- New - In the very first state of the thread lifecycle, the thread instance is created, and the start() method is yet to be invoked. The thread is considered alive now. - Runnable - After invoking the start() method, but before invoking the run() method, a thread is in the runnable state. A thread can also return to the runnable state from waiting or sleeping state. - Running - The thread enters the running state after the run() method is invoked. This is when the thread begins execution. - Non-Runnable - Although the thread is alive, it is not able to run. Typically, it returns to the runnable state after some time. - Terminated - The thread enters the terminated state once the run() method completes its execution. It is not alive now.

How does JIT compiling work

A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set. A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently. This is in contrast to a traditional compiler that compiles all the code to machine language before the program is first run. To paraphrase, conventional compilers build the whole program as an EXE file BEFORE the first time you run it. For newer style programs, an assembly is generated with pseudocode (p-code). Only AFTER you execute the program on the OS (e.g., by double-clicking on its icon) will the (JIT) compiler kick in and generate machine code (m-code) that the Intel-based processor or whatever will understand.

What is an Interface?

A Java interface is a template that has only method declarations and not method implementations. It is a workaround for achieving multiple inheritances in Java. Some worth remembering important points regarding Java interfaces are: - A class that implements the interface must provide an implementation for all methods declared in the interface - All methods in an interface are internally public abstract void - All variables in an interface are internally public static final - Classes do not extend but implement interfaces

What are static methods and variables

A class has two sections one declares variables, and other declares method, and these are called instance variables and instance methods, respectively. They are termed so because every time a class is instantiated, a new copy of each of them is created. Variables and methods can be created that are common to all objects and accessed without using a particular object by declaring them static. Static members are also available to be used by other classes and methods.

Class

A group of similar entities

What is an abstract method?

A method in Java that only has the declaration and not implementation is known as an abstract method. Also, an abstract method name is followed by the abstract keyword. Any concrete subclass that extends the abstract class must provide an implementation for abstract methods.

Aggregation

All objects have their separate lifecycle, but ownership is present. No child object can belong to some other object except for the parent object

Composition

Also called the death relationship, it is a specialized form of aggregation. Child objects don't have a lifecycle. As such, they automatically get deleted if the associated parent object is deleted

Rules for default constructors

Although the user can explicitly create a constructor, it is created on its own as soon as a class is created. If an explicitly-created constructor has a parameter, then it is necessary to create another constructor without a parameter.

What is an abstract class?

An abstract class in Java is a class that can't be instantiated. Such a class is typically used for providing a base for subclasses to extend as well as implementing the abstract methods and overriding or using the implemented methods defined in the abstract class. To create an abstract class, it needs to be followed by the abstract keyword. Any abstract class can have both abstract as well as non-abstract methods.

Instance variables

Are accessible to all methods in a class declared inside a class but outside a method Even when not assigned, instance variables have a value that can be null, 0, 0.0, or false For creating instance variables, the new keyword must be used.

Differentiate between break and continue

Break: used for loops as well as switch statements. Terminates the loop or switch block Continue: used only for loops Continues to the next iteration, skipping the current

Abstract Class vs Interface

Constituents - An abstract class contains instance variables, whereas an interface can contain only constants. Constructor and Instantiation - While an interface has neither a constructor nor it can be instantiated, an abstract class can have a default constructor that is called whenever the concrete subclass is instantiated. Implementation of Methods - All classes that implement the interface need to provide an implementation for all the methods contained by it. A class that extends the abstract class, however, doesn't require implementing all the methods contained in it. Only abstract methods need to be implemented in the concrete subclass. Type of Methods - Any abstract class has both abstract as well as non-abstract methods. Interface, on the other hand, has only a single abstract method.

String vs StringBuilder vs StringBuffer

Criteria to choose among String, StringBuffer and StringBuilder - If your text is not going to change use a string Class because a String object is immutable. If your text can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.

2 Ways to Create Threads

Either extend the thread class and override the run method. Disadvantage: you can't extend any other class Implement the Runnable interface

Explain about Java Virtual Machine

Explain about Java Virtual Machine

What is multiple inheritance? Does Java support multiple inheritance? If not, how can it be achieved?

If a subclass or child class has two parent classes, that means it inherits the properties from two base classes, it is multiple inheritances. Java does not multiple inheritances as in case if the parent classes have the same method names, then at runtime, it becomes ambiguous, and the compiler is unable to decide which method to execute from the child class.

What does the final keyword mean on a class

It can't be extended

What does the final keyword mean on a method

It cannot be overridden by a subclass

What does the final keyword mean on a variable

It cannot change value -- behaves like a constant

Encapsulation

Refers to the wrapping up of data and code into a single entity. Allows the variables of a class to be only accessible by the parent class and no other classes

Why is Java a platform independent language

Java language was developed in such a way that it does not depend on any hardware or software due to the fact that the compiler compiles the code and then converts it to platform-independent byte code which can be run on multiple systems.

Why is Java not a pure object oriented language

Java supports primitive data types - byte, boolean, char, short, int, float, long, and double and hence it is not a pure object-oriented language.

Method overriding

Method Overriding in Java allows a subclass to offer a specific implementation of a method that has already been provided by its parent or superclass. must have: - The same name - The same argument - The same return type

Abstraction explained

Representing essential features without the need to give out background details. The technique is used for creating a new suitable data type for some specific application

Rules for protected modifier in java

No: different package non-subclass Yes: all others

Local variables

Variables that are only accessible to the method or code block in which they are declared declared inside a method or a code block need to be assigned a value, where failing to assign a value will yield an error automatically created when a method is called and destroyed as soon as the method exits.

Inheritance

When an object acquires the properties of some other object, it is called inheritance. It results in the formation of a parent-child relationship amongst classes involved. Offers a robust and natural mechanism of organizing and structuring software

Method overloading

must have: - The same name - different arguments Return type may or may not be the same

What are the 4 access modifiers in Java

public private default protected

Rules for private modifier in java

yes: Same Class No: all others

Rules for default modifier in java

yes: Same Class, Same Package Subclass, Same Package Non-Subclass No: Different package subclass, Different package non-subclass


Kaugnay na mga set ng pag-aaral

A and P Sorry, no manipulations with clipboard allowed

View Set

CISSP PRACTICE TESTS Chapter 3 ▪Security Engineering (Domain 3)

View Set

Sadlier-Oxford Vocab Level H - Unit 12

View Set

Agenda Setting Theory: Chapter 30

View Set