Java 101

Ace your homework & exams now with Quizwiz!

logical operators

&& [and] || [or] ! [not]

Q33. What is the difference between break and continue statements? break continue1. Can be used in switch and loop (for, while, do while) statements 1. Can be only used with loop statements 2. It causes the switch or loop statements to terminate the moment it is executed

2. It doesn't terminate the loop but causes the loop to jump to the next iteration 3. It terminates the innermost enclosing loop or switch immediately 3. A continue within a loop nested with a switch will cause the next loop iteration to execute

Q26. Define a Java Class.

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

wrapper class

A class that contains a primitive type value, such as Integer, Double, Character and Boolean.

abstract method

A method that can be declared without any implementation.

final keyword

A modifier that specifies a constant.

Q3. What is abstraction in Java?

Abstraction refers to the quality of dealing with ideas rather than events. It basically deals with hiding the details and showing the essential things to the user. Thus you can say that abstraction in Java is the process of hiding the implementation details from the user and revealing only the functionality to them. Abstraction can be achieved in two ways: 1.Abstract Classes (0-100% of abstraction can be achieved) 2.Interfaces (100% of abstraction can be achieved)

Q34. What is an infinite loop in Java? Explain with an example.

An infinite loop is an instruction sequence in Java that loops endlessly when a functional exit isn't met. This type of loop can be the result of a programming error or may also be a deliberate action based on the application behavior. An infinite loop will terminate automatically once the application exits.

Q4. What do you mean by an interface in Java?

An interface in Java is a blueprint of a class or you can say it is a collection of abstract methods and static constants. In an interface, each method is public and abstract but it does not contain any constructor. Thus, interface basically is a group of related methods with empty bodies

Q27. What is an object in Java and how is it created?

An object is a real-world entity that has a state and behavior. An object has three characteristics:1.State2.Behavior3.Identity. An object is created using the 'new' keyword.

Array

ArrayList

Arrays are not type parameterized

Arraylists are type

Cannot contain values of different data types

Can contain values of different data types.

Creates a proper hierarchical structure which makes it easier to locate the related classes

Q19. Explain the term "Double Brace Initialization" in Java?

Double Brace Initialization is a Java term that refers to the combination of two independent processes. There are two braces used in this. The first brace creates an anonymous inner class. The second brace is an initialization block. When these both are used together, it is known as Double Brace Initialization. The inner class has a reference to the enclosing outer class, generally using the 'this' pointer. It is used to do both creation and initialization in a single statement. It is generally used to initialize collections. It reduces the code and also makes it more readable.

What is the difference between equals() and == in Java?

Equals() method is defined in Object class in Java and used for checking equality of two objects defined by business logic."==" or equality operator in Java is a binary operator provided by Java programming language and used to compare primitives and objects. public boolean equals(Object o) is the method provided by the Object class. The default implementation uses == operator to compare two objects. For example: method can be overridden like String class. equals() method is used to compare the values of two objects.

Why is synchronization necessary?

Explain with the help of a relevant example. Java allows multiple threads to execute. They may be accessing the same variable or object. Synchronization helps to execute threads one after another. It is important as it helps to execute all concurrent threads while being in sync. It prevents memory consistency errors due to access to shared memory. An example of synchronization code is as we have synchronized this function, this thread can only use the object after the previous thread has used it.

Why are strings immutable in java?

Identical String literals are collected in the "String pool" in an effort to conserve memory. Reference variables will then point to the same String object instance. Changing the object's state in the String pool will make changes to all references to that String object. Instead, when a change to a String is made, the JVM makes a new String object, and the reference variable points to the new String in the String pool. This helps with Memory saving and Security and they are thread safe.

Q44. What is a Map in Java?

In Java, Map is an interface of Util package which maps unique keys to values. The Map interface is not a subset of the main Collection interface and thus it behaves little different from the other collection types. Below are a few of the characteristics of Map interface: 1.Map doesn't contain duplicate keys. 2.Each key can map at max one value.

Q30. What is the difference between a local variable and an instance variable?

In Java, a local variable is typically used inside a method, constructor, or a block and has only local scope. Thus, this variable can be used only within the scope of a block. The best benefit of having a local variable is that other methods in the class won't be even aware of that variable.

Q25. What are access modifiers in Java?

In Java, access modifiers are special keywords which are used to restrict the access of a class, constructor, data member and method in another class. Java supports four types of access modifiers:1.Default2.Private3.Protected4.Public

What are constructors in Java?

In Java, constructor refers to a block of code which is used to initialize an object. It must have the same name as that of the class. Also, it has no return type and it is automatically called when an object is created.There are two types of constructors:1. Default Constructor: In Java, a default constructor is the one which does not take any inputs. In other words, default constructors are the no argument constructors which will be created by default in case you no other constructor is defined by the user. Its main purpose is to initialize the instance variables with the default values. Also, it is majorly used for object creation. 2. Parameterized Constructor: The parameterized constructor in Java, is the constructor which is capable of initializing the instance variables with the provided values. In other words, the constructors which take the arguments are called parameterized constructors.

Q2. What is runtime polymorphism or dynamic method dispatch?

In Java, runtime polymorphism or dynamic method dispatch is a process in which a call to an overridden method is resolved at runtime rather than at compile-time. In this process, an overridden method is called through the reference variable of a superclass. Let's take a look at the example below to understand it better.

Q42. Why Java Strings are immutable in nature?

In Java, string objects are immutable in nature which simply means once the String object is created its state cannot be modified. Whenever you try to update the value of that object instead of updating the values of that particular object, Java creates a new string object. Java String objects are immutable as String objects are generally cached in the String pool. Since String literals are usually shared between multiple clients, action from one client might affect the rest. It enhances security, caching, synchronization, and performance of the application.

Q35. What is the difference between this() and super() in Java?

In Java, super() and this(), both are special keywords that are used to call the constructor. this() super() 1. this() represents the current instance of a class 1. super() represents the current instance of a parent/base class 2. Used to call the default constructor of the same class 2. Used to call the default constructor of the parent/base class 3. Used to access methods of the current class 3. Used to access methods of the base class 4. Used for pointing the current class instance 4. Used for pointing the superclass instance 5. Must be the first line of a block 5. Must be the first line of a block

Q45. What is collection class in Java? List down its methods and interfaces.

In Java, the collection is a framework that acts as an architecture for storing and manipulating a group of objects. Using Collections you can perform various tasks like searching, sorting, insertion, manipulation, deletion, etc. Java collection framework includes the following: Interfaces Classes Methods

When can you use the super keyword?

In Java, the super keyword is a reference variable that refers to an immediate parent class object.

Method Overriding

In Method Overriding, the subclass has the same method with the same name and exactly the same number and type of parameters and same return type as a superclass.Method Overriding is to "Change" existing behavior of the method.It is a run time polymorphism.The methods must have the same signature.It always requires inheritance in Method Overriding.

Q6. What is inheritance in Java?

Inheritance in Java is the concept where the properties of one class can be inherited by the other. It helps to reuse the code and establish a relationship between different classes. Inheritance is performed between two types of classes: 1.Parent class (Super or Base class)2.Child class (Subclass or Derived class)A class which inherits the properties is known as Child Class whereas a class whose properties are inherited is known as Parent class.

Q5. What is the difference between abstract classes and interfaces? Abstract Class

Interfaces An abstract class can provide complete, default code and/or just the details that have to be overridden. An interface cannot provide any code at all, just the signature In the case of an abstract class, a class may extend only one abstract class.A Class may implement several interfaces.An abstract class can have non-abstract methods.All methods of an Interface are abstract.An abstract class can have instance variables.An Interface cannot have instance variables.An abstract class can have any visibility: public, private, protected. An Interface visibility must be public (or) none. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly. If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. An abstract class can contain constructors. An Interface cannot contain constructors. Abstract classes are fast. Interfaces are slow as it requires extra indirection to find the corresponding method in the actual class

Q24. What is JIT compiler in Java?

JIT stands for Just-In-Time compiler in Java. It is a program that helps in converting the Java bytecode into instructions that are sent directly to the processor. By default, the JIT compiler is enabled in Java and is activated whenever a Java method is invoked. The JIT compiler then compiles the bytecode of the invoked method into native machine code, compiling it "just in time" to execute. Once the method has been compiled, the JVM summons the compiled code of that method directly rather than interpreting it. This is why it is often responsible for the performance optimization of Java applications at the run time.

Q36. What is Java String Pool?

Java String pool refers to a collection of Strings which are stored in heap memory. In this, whenever a new object is created, String pool first checks whether the object is already present in the pool or not. If it is present, then the same reference is returned to the variable else new object will be created in the String pool and the respective reference will be returned.

Q23. Why pointers are not used in Java?

Java doesn't use pointers because they are unsafe and increases the complexity of the program. Since, Java is known for its simplicity of code, adding the concept of pointers will be contradicting. Moreover, since JVM is responsible for implicit memory allocation, thus in order to avoid direct access to memory by the user, pointers are discouraged in Java.

Why Java is platform independent?

Java is called platform independent because of its byte codes which can run on any system irrespective of its underlying operating system.

Why Java is not 100% Object-oriented?

Java is not 100% Object-oriented because it makes use of eight primitive data types such as boolean, byte, char, int, float, double, long, short which are not objects.

Q7. What are the different types of inheritance in Java?

Java supports four types of inheritance which are:1.Single Inheritance: In single inheritance, one class inherits the properties of another i.e there will be only one parent as well as one child class.2.Multilevel Inheritance: When a class is derived from a class which is also derived from another class, i.e. a class having more than one parent class but at different levels, such type of inheritance is called Multilevel Inheritance.3.Hierarchical Inheritance: When a class has more than one child classes (subclasses) or in other words, more than one child classes have the same parent class, then such kind of inheritance is known as hierarchical.4.Hybrid Inheritance: Hybrid inheritance is a combination of two or more types of inheritance.

Q8. What is method overloading and method overriding?

Method Overloading :In Method Overloading, Methods of the same class shares the same name but each method must have a different number of parameters or parameters having different types and order.Method Overloading is to "add" or "extend" more to the method's behavior.It is a compile-time polymorphism.The methods must have a different signature.It may or may not need inheritance in Method Overloading

Q31. Differentiate between the constructors and methods in Java?

Methods Constructors 1.Used to represent the behavior of an object 1. Used to initialize the state of an object2. Must have a return type 2. Do not have any return type3. Needs to be invoked explicitly3. Is invoked implicitly4. No default method is provided by the compiler4. A default constructor is provided by the compiler if the class has none5. Method name may or may not be same as class name 5. Constructor name must always be the same as the class name

Need to specify the index in order to add data

No need to specify the index

Q28. What is Object Oriented Programming?

Object-oriented programming or popularly known as OOPs is a programming model or approach where the programs are organized around objects rather than logic and functions. In other words, OOP mainly focuses on the objects that are required to be manipulated instead of logic. This approach is ideal for the programs large and complex codes and needs to be actively updated or maintained.

Packages can also contain hidden classes which are not visible to the outer classes and only used within the package

Packages help in avoiding name clashes

Q22. What is a package in Java? List down various advantages of packages.

Packages in Java, are the collection of related classes and interfaces which are bundled together. By using packages, developers can easily modularize the code and optimize its reuse. Also, the code within the packages can be imported by other classes and reused. Below I have listed down a few of its advantages:

Q1. What is Polymorphism?

Polymorphism is briefly described as "one interface, many implementations". Polymorphism is a characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form. There are two types of polymorphism: 1.Compile time polymorphism 2.Run time polymorphism Compile time polymorphism is method overloading whereas Runtime time polymorphism is done using inheritance and interface.

What is the importance of reflection in Java?

Reflection is a runtime API for inspecting and changing the behavior of methods, classes, and interfaces. Java Reflection is a powerful tool that can be really beneficial. Java Reflection allows you to analyze classes, interfaces, fields, and methods during runtime without knowing what they are called at compile time. Reflection can also be used to create new objects, call methods, and get/set field values. External, user-defined classes can be used by creating instances of extensibility objects with their fully-qualified names. Debuggers can also use reflection to examine private members of classes.

What is singleton class in Java and how can we make a class singleton?

Singleton class is a class whose only one instance can be created at any given time, in one JVM. A class can be made singleton by making its constructor private.

Size must be defined at the time of declaration

Size can be dynamically changed

Q37. Differentiate between static and non-static methods in Java.

Static Method Non-Static Method 1. The static keyword must be used before the method name 1. No need to use the static keyword before the method name 2. It is called using the class (className.methodName) 2. It is can be called like any general method 3. They can't access any non-static instance variables or methods 3. It can access any static method and any static variable without creating an instance of the class

reference data types or Non primitive data types

String

Q41. What is a classloader in Java?

The Java ClassLoader is a subset of JVM (Java Virtual Machine) that is responsible for loading the class files. Whenever a Java program is executed it is first loaded by the classloader. Java provides three built-in classloaders:1.Bootstrap ClassLoader2.Extension ClassLoader3.System/Application ClassLoader

How to not allow serialization of attributes of a class in Java?

The Non-Serialized attribute can be used to prevent member variables from being serialized.You should also make an object that potentially contains security-sensitive data non-serializable if possible. Apply the Non-Serialized attribute to certain fields that store sensitive data if the object must be serialized. If you don't exclude these fields from serialization, the data they store will be visible to any programs with serialization permission.

Polymorphism

The ability of an object to identify as more than one type.

method overloading

The ability to define two or more different methods with the same name but different method signatures.

autoboxing

The automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper class

Q20. Why is it said that the length() method of String class doesn't return accurate results?

The length() method of String class doesn't return accurate results because it simply takes into account the number of characters within in the String. In other words, code points outside of the BMP (Basic Multilingual Plane), that is, code points having a value of U+10000 or above, will be ignored. The reason for this is historical. One of Java's original goals was to consider all text as Unicode; yet, Unicode did not define code points outside of the BMP at the time. It was too late to modify char by the time Unicode specified such code points.

Q21. What are the differences between Heap and Stack Memory in Java?

The major difference between Heap and Stack memory are:

They provide easier access control on the code

How is the creation of a String using new() different from that of a literal?

When we create a string using new(), a new object is created. Whereas, if we create a string using the string literal syntax, it may return an already existing object with the same name.

What are wrapper classes in Java?

Wrapper classes convert the Java primitives into the reference types (objects). Every primitive data type has a class dedicated to it. These are known as wrapper classes because they "wrap" the primitive data type into an object of that class. Refer to the below image which displays different primitive type, wrapper class and constructor argument.

Q9. Can you override a private or static method in Java?

You cannot override a private or static method in Java. If you create a similar method with the same return type and same method arguments in child class then it will hide the superclass method; this is known as method hiding. Similarly, you cannot override a private method in subclass because it's not accessible there. What you can do is create another private method with the same name in the child class.

Generics

_________ provide flexible type safety to your code, reduce the need for casting with collections and move many common errors from run time to compile time

method

a block of code this executed whenever it is called upon

2D ArrayList

a dynamic list of list that you can change the size of during runtime.

while loop

a programming construct used to repeat a set of commands (loop) as long as (while) a boolean condition is true

static keyword

a single copy of a variable/method is created and shared basically the class "owns" the static variable or method

constructor

a special that is called when a object is created

Interfaces

a template that can be applied to a class. Similar to inheritance but specifies what a class has/must do. Classes can apply more than one interface but inheritance is limited to 1 super.

Benefit of abstraction

adds a layer of security to your program this can be provided to classes and methods.

Objects

an instance of a class that may contain attribute and methods

primitive data types

byte, short, int, long, float, double, char, boolean

super keyword

calls the parent class of an object. Works similar to "This" keyword

Abstract classes

cannot be instantiated directly but they can have a subclass

static method

dont require you to create an instance, you just type the className.methodName() to access it.

Q32. What is final keyword in Java?

final is a special keyword in Java that is used as a non-access modifier. A final variable can be used in different contexts such as: final variable. When the final keyword is used with a variable then its value can't be changed once assigned. In case the no value has been assigned to the final variable then using only the class constructor a value can be assigned to it.

ArrayList

is a resizeable array that can be stored into a reference data type.

Expressions

operands and operators

Operators

special tokens that represent computations like addition, multiplication and division

variable

stores a value

unboxing

the reverse of autoboxing. Automatic conversion of wrapper class to primitive.

Operands

values, variables, numbers, quantity


Related study sets

FD- Culturally Competent Nursing Care + Culture

View Set

Acute and Chronic Wound Management

View Set

International Business Chapter 5

View Set

Chapter 7: Mental Images and Propositions

View Set