- Tell me five features of Java.
Ans: Following are the features of Java.
1. Java is simple to use.
2. Java is an object-oriented programming language.
3. Java provides good security.
4. Java is platform-independent.
5. It is multithreaded.
6. Java is a portable language.
- Why is Java a partially orientated programming language?
Ans: There are seven properties to be satisfied with an object-oriented programming language.
1. Encapsulation/Data Hiding
5. All user-defined types are objects
6. All predefined types are objects
7. All operations performed on objects must be only through methods exposed to the objects.
But Java only supports the first five properties. It fails to supports the last two properties. That’s why Java is a partially oriented programming language.
- Where do we use Java?
Ans: Java is a multipurpose programming language. It is used in various fields. With the help of Java, we can develop application servers, website applications, Mobile applications, Enterprise applications, and games. Also, Java supports data science, machine learning and Artificial intelligence.
- Who is the father of Java?
Ans: James Gosling developed Java at Sun Microsystem Inc. in 1991.
- What is the difference between Java and c++?
Ans: The difference between Java and c++ is as follows.
|All methods are part of the class.
|Can have stand-alone methods.
|Does not support multiple inheritance.
|Supports multiple inheritance.
|The main method can not return a value.
|The main method can return a value.
|Does not have interfaces.
|No forward referencing required.
|Explicit forward referencing required.
|Compiled and interpreted.
|Does not support operator overloading.
|supports operator overloading.
|Strings are objects.
|Strings are null-terminated character arrays.
- What is public static void main(String arg) in Java?
Ans: The public static void main(String arg) is the entry point of all Java programs. For this method, all program starts execution of the code.
- Which are the three OOP principles of Java?
Ans: All object-oriented programming languages provide mechanisms that help you to implement the object-oriented model. They are encapsulation, inheritance and polymorphism.
- What is encapsulation?
Ans: An encapsulation is a mechanism that binds together code and the data it manipulates and keeps both safes from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface.
- What is polymorphism?
Ans: Polymorphism is a Greek word. If we break the word into poly and morphism, then it means many forms.
- What is a command to execute and run the Java program from the command prompt?
Ans: For compiling a Java program, The command is-
To run the Java program, The command is-
- How many keywords are reserved in Java?
Ans: There are 49 keywords reserved in Java.
- What does the Java virtual machine do?
Ans: The Java virtual machine calls the main method is the Java program and enables it to run. JVM compiles the Java program, and after compilation, it is converted into bytecode.
- What is Javadoc?
Ans: The Javadoc tool generates HTML pages of API documentation from Java source files. It parses the declarations and documentation comments in a set of Java source files. It produces a corresponding set of HTML pages describing the public and protected classes, inner classes, interfaces, constructors, methods, and fields. Javadoc can be used on entire packages, individual source files, or both.
- What is the difference between System.out.print() and System.out.println()?
Ans: Both of the functions are used to print the result on the console. The print() function keeps the cursor on the same line, and the println() function moves it to the next line.
- Name two Java IDE’s
Ans: 1) NetBeans sponsored by Sun/ Oracle.
2) Eclipse sponsored by IBM.
- What is the use of final variables in Java?
Ans: A final variable is a constant whose value cannot be changed. The final keyword indicates that a local variable or instance variable cannot be altered. A final variable must be initialized, and once initialized, it cannot be changed. Any attempt to alter it results in a compile-time error.
- What are the command line arguments?
Ans: The main method is defined as a method that accepts an array of strings as command-line arguments. These are the arguments passed to the main function when it starts executing. Input values can be passed to the main from the command line and accessed in the code.
- What is a class?
Ans: A class is a template that contains the data variables and the methods that operate on those data variables.
- Explain types of classes.
Ans: There are two types of classes in Java.
Concrete class: A concrete class is a class that can be instantiated. It is a complete class that defines data members, defines constructors, defines methods (member functions) and can also be subclassed.
Abstract class: An abstract class is an incomplete class that cannot be instantiated. It often defines data members, defines constructors, defines some methods, declares other methods.
- What is a constructor in Java?
Ans: In most cases, it is required that as soon as an instance (or object) is created, its members should be initialized. Java allows objects to initialize themselves when they are created. This automatic initialization is performed through the use of a constructor. A constructor is a special method of the class with the same name and is used to initialize the object members. Once defined, the constructor is automatically called immediately after the object is created.
- What are the types of constructor in Java?
Ans: There are two types of constructors in Java.
1. Default Constructor
This is a constructor with no parameters. If you don’t define a constructor for a class, it is automatically created by the compiler. However, if you define any constructor for your class, a default constructor is no longer automatically created.
2. Parameterized Constructor
Java supports method overloading. This means that two or more methods can have the same signature within the same scope. We can add another constructor to the class, which will assign user given values to the members. The job of the parameterized constructor is to assign specific values to members.
- What is this keyword in Java, and what are the uses?
Ans: The object which invokes a method is called the implicit or hidden object. Sometimes a method may need to refer to this implicit object. To allow this, Java defines this keyword. This keyword always refers to the current or implicit object.
Uses of this:
1. To resolve ambiguity between a local and instance variable: When the local variable has the same name as the instance variable, the local variable hides the instance variable. Hence, this can be used to specify instance variables.
2. To return the implicit object: In some cases, a method may want to return the implicit object itself. In such a case, this can be returned.
3. To invoke methods and access instance members: Instance members and methods can be accessed using this inside a method.
4. To invoke one constructor within another: From within a constructor, you can also use the this keyword to call another constructor in the same class. Doing so is called an explicit constructor invocation.
- What is a static block in Java?
Ans: In addition to static fields and methods, a class can have a static block that belongs to the class. This block will be executed even before the main is invoked. This block is executed only once when the class is loaded. Its main purpose is to initialize static variables. The following program demonstrates a static block.
- What is the wrapper class in Java?
Ans: A wrapper class is a predefined class whose object encapsulates a single value of a primitive type.
- What is an inner class in Java?
Ans: In Java, it is possible to define a class within another class; such classes are known as nested classes. Thus, if class B is defined within class A, A is also called an outer class or enclosing class, while B is the nested class. A nested class is a member of the enclosing class, just like any other member.
The syntax is,
// variables and methods of the outer class
// variables and methods of the nested class
Since a nested class does not exist outside the outer class, an object of the nested class should be created inside the outer class itself. The following code illustrates this. In such a case, the nested class instance exists within the outer class instance. The nested class object stores a reference to the outer class object and hence can access all its members.
- What is the difference between static and inner class?
Ans: The difference between static and inner class is as follows.
|A Static nested class belongs to the outer class and not an instance of the outer class.
|An inner class belongs to an instance of the outer class.
|A static class can access static members of the outer class directly.
|An inner class cannot access.
|A static class can access non-static members of the outer class using an object.
|An inner class can access non-static members of the outer class directly.
|A static class can define a static member or method.
|An inner class cannot define a static member or method.
- What is inheritance?
Ans: Inheritance is the process by which one object acquires the properties of another object. by the use of inheritance, an object needs only define to those qualities that make it unique within its class. It can inherit its general attributes from its parent. Thus, it is the inheritance mechanism that makes it possible for one object to be a specific instance of a more general case.
- Explain types of inheritance.
Ans: Java supports single, multilevel and hierarchical inheritance. Unlike C++, Java does not support multiple inheritance.
1. Single Inheritance: In this type of inheritance, a class can have only one superclass, i.e. it can only be inherited from one class.
2. Multilevel Inheritance: In multilevel inheritance, a subclass is further used to derive more classes. For example, class B extends A, and C extends class B.
3. Hierarchical Inheritance: A single superclass class has many subclasses. These are further used to create more subclasses.
- What is the use of super keyword in Java?
Ans: it is used for two purposes
1. Invoking a superclass constructor.
If the superclass has a default constructor, the subclass need not define a constructor. However, if the superclass defines a parameterized constructor, the subclass must define a parameterized constructor and pass parameters to the superclass constructor.
2. Invoking superclass members hidden by a subclass.
If the subclass defines members having the same name as the superclass, it hides the superclass members. Hence, the superclass members cannot be accessed in the subclass. If you want to access them, the super keyword should be used.
- What do you understand by method overloading?
Ans: Method overloading means two or more methods having the same name but differing in many parameters or types.
- Explain the concept of dynamic linking.
Ans: When methods are overridden in multilevel inheritance, Java resolves overridden methods during runtime and not during compile-time. This is called dynamic linking.
- What is an abstract class, and what does it contain?
Ans: In an inheritance hierarchy, the subclasses are the specialized classes, while the superclasses are the more general classes. This means that the class at the topmost in the hierarchy is the most general class. We may not want to create any objects of this general class. The only purpose it serves is to create subclasses that add more features to this class.
An abstract class can contain:
1. Data members
3. Static members and methods
4. Method definition
5. Abstract methods (only declaration)
- What is an abstract method?
Ans: An abstract class provides all the functionality required by all its subclasses. However, we may not define all its methods because the abstract class may not know how to define those methods. An abstract method has to be overridden in the subclass. A class that has an abstract method must be declared as abstract.
- What is the use of the final keyword in Java?
Ans: In some cases, we may not want a class to be inherited, or we may not want a superclass method to be redefined in a subclass.
The final keyword is used for these two purposes.
1. Using Final to Prevent Method Overriding
When we do not want a subclass to override a superclass method, the superclass should be declared final. This ensures that the subclass uses only the superclass method.
2. Using Final to Prevent Inheritance
A class that is declared final cannot be inherited. When a class is declared final, all its methods are implicitly final too.
- What is the difference between interface and abstract class?
Ans: The difference between interface and abstract class is as follows.
|Pure abstract class.
|It specifies functionality which a class should have.
|It specifies common attributes and behavior that a class inherits.
|Contains only method declarations.
|Contains method declarations and definitions. Fields can be static, final as well as instance fields.
|Fields are implicitly final and static.
|Fields can be static, final and instance fields.
|It can not have a constructor.
|Can have a constructor.
|Used with the implements keyword.
|Used with the extends keyword.
|It can be implemented by more than one class.
|It can be extended by more than one class.
|A class can implement more than one interface.
|A class can extend only one abstract class.
|Another interface can extend an interface.
|An abstract class can be extended by another abstract as well as a concrete class.
|It can be used to implement multiple inheritance.
|A class cannot extend more than one class.
- Explain try catch block.
Ans: Program statements that you want to monitor for exceptions are written within the try block. If an exception occurs within the try block, an exception object will be created and thrown. Immediately following the try block, a catch block specifies the exception type you wish to catch. One or more catch blocks can follow a try block for handling multiple exception types. Any code that absolutely must be executed is put in the finally block.
- Can we write multiple catch blocks in Java?
Ans: Yes, We can handle different exceptions in multiple catch blocks.
- Explain different types of streams in Java.
Ans. Java supports two types of the stream:
1. Byte-Oriented Stream: Used for reading and writing data in the form of raw bytes.
2.Character Oriented Stream: Used for reading and writing characters (Unicode). Only a low-level stream capable of reading bytes or characters can be directly connected to an I/O device.
- What is the difference between == and .equals in Java?
Ans: The == operator is used to compare the address of objects and the .equals() method is used to compare the content in the object.
- Tell me any three methods of file class and use of it.
Ans: 1. String getName() – It returns the name of the file object.
2. boolean isFile() – It returns true if the file object represents a file and false otherwise.
3. boolean delete() – It deletes the file or directory associated with the file object.
- What are the features of swing?
Ans: The features of swing are-
1. Swing does not rely on the underlying platform for creating and managing the user interface components.
2. Swing components are written in Java, and hence, they are lightweight components.
3. Swing components look and feel the same on different platforms. They give a consistent user experience across all platforms.
4. The swing library is independent of the underlying platform’s GUI toolkit. Hence, it can support more components than the underlying platform.
5. Well suited for complex GUI applications.
- What are the characteristics of MVC architecture?
Ans: In the MVC architecture, every component has three characteristics:
1. Its content, such as the state of a button (pushed in or not), or the text in a text field.
2. Its visual appearance is colour, size, and so on.
3. Its behaviour specifies its reaction to events.
- Explain init() and paint() methods of the applet.
Ans: The applet class defines four methods that control the creation and execution of an applet.
1. init(): This method is the first method to be invoked for an applet. It is used to perform any initialization needed by the applet. It is automatically invoked by the system when Java first launches the applet. It typically does things like reading parameters, load images etc. Applets can have a default constructor, but it is customary to perform all initializations in the init() method.
2. paint(): This method is used whenever the applet needs to be redrawn in the browser window. This method has one parameter of type Graphics which describes the graphics environment in which the applet is running. The paint method is also used to display text, graphics objects etc., on the applet.
- What is the difference between AWT and Swing?
Ans: The difference between AWT and swing are as follows.
|Original GUI package. Supported since JDK 1.1
|Newer GUI package. Supported by JDK 1.2
|AWT uses the peer approach to render GUI components
|Swing obtains a top-level window and “paints” components on the window.
|AWT components are created and displayed by the native GUI toolkit.
|Swing components are created and displaye
d by Java.
|Heavy dependency on the underlying Platform.
|Less dependency on the underlying platform.