java

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Can we have multiple public classes in a java source file?

We can't have more than one public class in a single java source file. A single source file can have multiple classes that are not public.

Can we have try without catch block?

Yes, we can have try-finally statement and hence avoiding catch block.

What is Enum in Java?

a new type whose fields consists of fixed set of constants

What is anonymous inner class?

A local inner class without name is anonymous inner class. it is defined and instantiated in a single statement. it always extends a class or implements an interface. Since an anonymous class has no name, it is not possible to define a constructor for it. Anonymous inner classes are accessible only at the point where it is defined.

What is an abstract class?

Abstract classes are used in java to create a class with some default method implementation for subclasses. An abstract class can have abstract method without body and it can have methods with implementation also. Abstract classes can't be instantiated and mostly used to provide base for sub-classes to extend and implement the abstract methods and override or use the implemented methods.

What is composition in java?

Composition is the design technique to implement has-a relationship in classes. We can use Object composition for code reuse. Java composition is achieved by using instance variables that refer to other objects. compared with inheritance, The benefit of using composition is that we can control the visibility of other objects to client classes and reuse only what we need.

What is Garbage Collection?

Garbage Collection is the process of looking at heap memory, identifying which objects are in use and which are not, and deleting the unused objects. In Java, process of deallocating memory is handled automatically by the garbage collector.

differences between Heap and Stack memory.

Heap memory is used by all the parts of the application whereas stack memory is used only by one thread of execution. Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space. Objects stored in the heap are globally accessible whereas stack memory can't be accessed by other threads. Memory management in stack is done in LIFO manner whereas it's more complex in Heap memory because it's used globally. Heap memory is divided into Young-Generation, Old-Generation etc. Stack memory is short-lived whereas heap memory lives from the start till the end of application. When stack memory is full, Java runtime throws java.lang.StackOverFlowError whereas if heap memory is full, it throws java.lang.OutOfMemoryError error. Stack memory size is very less when compared to Heap memory.

Can an interface implement or extend another interface?

Interfaces don't implement another interface, they extend it. Since interfaces can't have method implementations, there is no issue of diamond problem. That's why we have multiple inheritance in interfaces.

What is an interface?

Interfaces provide a way to achieve abstraction in java and used to define the contract for the subclasses to implement. a java class can implements multiple interfaces

What is Java Annotations?

Java Annotations provide information about the code and they have no direct effect on the code they annotate.Java Built-in annotations are @Override, @Deprecated and @SuppressWarnings.

What is Classloader in Java?

Java Classloader is the program that loads byte code program into memory when we want to access any class.

What is the difference between JDK and JVM?

Java Development Kit (JDK) is for development purpose and JVM is a part of it to execute the java programs. JDK provides all the tools, executables and binaries required to compile, debug and execute a Java Program. The execution part is handled by JVM to provide machine independence.

Java Heap Space

Java Heap space is used by java runtime to allocate memory to Objects and JRE classes. Whenever we create an object, it's always created in the Heap space. Garbage Collection runs on the heap memory to free the memory used by objects that don't have any reference. Any object created in the heap space has global access and can be referenced from anywhere of the application.

What is the difference between JVM and JRE?

Java Runtime Environment (JRE) is the implementation of JVM. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn't contain any development tools like java compiler, debugger etc. If you want to execute any java program, you should have JRE installed.

Java Stack memory

Java Stack memory is used for the execution of a thread. They contain method-specific values that are short-lived and references to other objects in the heap. Stack memory is always referenced in LIFO (Last-In-First-Out) order. Whenever a method is invoked, a new block is created in the stack memory to hold local primitive values and reference to other objects in the method. As soon as the method ends, the block becomes unused and becomes available for the next method. Stack memory size is very less compared to Heap memory.

What is JVM and is it platform independent?

Java Virtual Machine (JVM) is the heart of java programming language. JVM is responsible for converting byte code into machine readable code. JVM is not platform independent, thats why you have different JVM for different operating systems.

Why Java doesn't support multiple inheritance?

Java doesn't support multiple inheritance in classes because of "Diamond Problem". However multiple inheritances are supported in interfaces. An interface can extend multiple interfaces because they just declare the methods and implementation will be present in the implementing class. So there is no issue of the diamond problem with interfaces.

Why Java is not pure Object Oriented language?

Java is not pure object-oriented because it has eight primitive types such as byte, short, int, long, float, double, char, boolean. it brings simplicity to the language while writing code. Obviously, java could have wrapper objects for the primitive types but just for the representation. As we know, for all the primitive types we have wrapper classes such as Integer, Long etc that provides some additional methods.

What is Java Package and which package is imported by default?

Java package is the mechanism to organize the java classes by grouping them. The grouping logic can be based on functionality or modularity. java.lang package is imported by default.

What are access modifiers?

Java provides access control through public, private and protected access modifier keywords. When none of these are used, it's called default access modifier.

What is static block?

Java static block is the group of statements that gets executed when the class is loaded into memory by Java ClassLoader. It is used to initialize static variables of the class. Mostly it's used to create static resources when class is loaded.

What is ternary operator in java?

Java ternary operator is the only conditional operator that takes three operands. It's a one line replacement for if-then-else statement and used a lot in java programming.

What is default constructor?

No argument constructor of a class is known as default constructor. When we don't define any constructor for the class, java compiler automatically creates the default no-args constructor.

What is the benefit of Composition over Inheritance?

One of the best practices of Java programming is to "favor composition over inheritance". Some of the possible reasons are: Any change in the superclass might affect subclass even though we might not be using the superclass methods. For example, if we have a method test() in the subclass and suddenly somebody introduces a method test() in the superclass, we will get compilation errors in the subclass. The composition will never face this issue because we are using only what methods we need. Inheritance exposes all the superclass methods and variables to the client and if we have no control in designing superclass, it can lead to security holes. Composition allows us to provide restricted access to the methods and hence more secure. We can get runtime binding in composition where inheritance binds the classes at compile time. So composition provides flexibility in the invocation of methods.

What is the use of System class?

System class is final so that we can't subclass and override its behavior through inheritance. System class doesn't provide any public constructors, so we can't instantiate this class and that's why all of its methods are static.

What is Serialization and Deserialization?

We can convert a Java object to an Stream that is called Serialization. Once an object is converted to Stream, it can be saved to file or send over the network or used in socket connections. The process of converting stream data created through serialization to Object is called deserialization.

What is inner class in java?

We can define a class inside a class and they are called nested classes. they can access all the variables and methods of the outer class. Since inner classes are associated with the instance, we can't have any static variables in them.

What is break and continue statement?

We can use break statement to terminate for, while, or do-while loop. We can use break statement in switch statement to exit the switch case. We can use break with label to terminate the nested loops. The continue statement skips the current iteration of a for, while or do-while loop. We can use the continue statement with the label to skip the current iteration of the outermost loop.

Can we declare a class as static?

We can't declare a top-level class as static however an inner class can be declared as static.

How to sort a collection of custom Objects in Java?

We need to implement Comparable interface to support sorting of custom objects in a collection. Comparable interface has compareTo(T obj) method which is used by sorting methods. However, if you want to sort based on different criteria, then we can create Comparator instances and pass it to the sort method as argument.

What is overloading and overriding in java?

When we have more than one method with the same name in a single class but the arguments are different, then it is called as method overloading. Overriding concept comes with inheritance when we have two methods with same signature, one in parent class and another in child class.

Can we overload main method?

Yes, we can have multiple methods with name "main" in a single class. However if we run the class, java runtime environment will look for main method with syntax as public static void main(String args[])

What is the difference between abstract class and interface?

abstract keyword is used to create abstract class whereas interface is the keyword for interfaces. Abstract classes can have method implementations whereas interfaces can't. A class can extend only one abstract class but it can implement multiple interfaces. We can run an abstract class if it has main() method whereas we can't run an interface.

What is final keyword?

final keyword is used with Class to make sure no other class can extend it, for example String class is final and we can't extend it. We can use the final keyword with methods to make sure child classes can't override it. final keyword can be used with variables to make sure that it can be assigned only once. However the state of the variable can be changed, for example, we can assign a final variable to an object only once but the object variables can change later on. Java interface variables are by default final and static.

What is finally and finalize in java?

finally block is used with try-catch to put the code that you want to get executed always, even if any exception is thrown by the try-catch block. finally block is mostly used to release resources created in the try block. finalize() is a special method in Object class that we can override in our classes. This method gets called by the garbage collector when the object is getting garbage collected.

What is the importance of main method in Java?

main() method is the entry point of any standalone java application. main method is public and static so that java can access it without initializing the class. The input parameter is an array of String through which we can pass runtime arguments to the java program.

What is static keyword?

static keyword can be used with class level variables to make it global i.e all the objects will share the same variable. static keyword can be used with methods also. A static method can access only static variables of class and invoke only static methods of the class.

What does super keyword do?

super keyword can be used to access super class method when you have overridden the method in the child class. We can use super keyword to invoke superclass constructor in child class constructor but in this case, it should be the first statement in the constructor method.

What is this keyword?

this keyword provides the reference to the current object and it's mostly used to make sure that object variables are used, not the local variables having the same name.


संबंधित स्टडी सेट्स

Chapter 1: Nutrition (Wiley Questions)

View Set

Nevada Statutes & Regulations Common to Life and Health Only

View Set

Chapter 6 bio photosynthesis and the Calvin cycle

View Set

Exam 2, Chapters 5, 7, 10, and 11

View Set