Java Question Set




1) What is Object-Oriented Programming (OOP)?

A type of programming in which programmers define not only the data type of a data structure, but also the types of operations (functions) that can be applied to the data structure. In this way, the data structure becomes an object that includes both data and functions. To perform object-oriented programming, one needs an object-oriented programming language (OOPL). Java, C++ and Smalltalk are three of the more popular languages.


2. What is a class?
A class describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It is a comprehensive data type, which represents a blue print of objects. It is a template of object.

A class can be defined as the primary building block of OOP. It also serves as a template that describes the properties, state, and behaviors common to a particular group of objects.

A class contains data and behavior of an entity. For example, the aircraft class can contain data, such as model number, category, and color and behavior, such as duration of flight, speed, and number of passengers. A class inherits the data members and behaviors of other classes by extending from them.




3. What is an object?
They are instance of classes. It is a basic unit of a system. An object is an entity that has attributes, behavior, and identity. Attributes and behavior of an object are defined by the class definition.



4. What is the relationship between a class and an object?
A class acts as a blue-print that defines the properties, states, and behaviors that are common to a number of objects. An object is an instance of the class. For example, you have a class called Vehicle and Car is the object of that class. You can create any number of objects for the class named Vehicle, such as Van, Truck, and Auto.

The new operator is used to create an object of a class. When an object of a class is instantiated, the system allocates memory for every data member that is present in the class.



5.  What is meant by variable?
Variables are locations in memory that can hold values. Before assigning any value to a variable, it must be declared.




6. What do you mean by Constructor?
Constructor gets invoked when a new object is created. Every class has a constructor. If we do not explicitly write a constructor for a class the java compiler builds a default constructor for that class.


5. What is the difference between a constructor and a method?
A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.
A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.



6. What is the purpose of garbage collection in Java, and when is it used?
The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused.
A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used.



7. State the significance of public, private, protected.
public: Public class is visible in other packages, field is visible everywhere (class must be public too)
private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.
protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature. This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.



8. What is static in java?
This means that you can use them without creating an instance of a class. Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. In other words, you can't change a static method into an instance method in a subclass.



9. Define Inheritance?
It is the process where one object acquires the properties of another. With the use of inheritance the information is made manageable in a hierarchical order.


10. What is Abstraction?
It refers to the ability to make a class abstract in OOP. It helps to reduce the complexity and also improves the maintainability of the system.


11. What is Abstract class
These classes cannot be instantiated and are either partially implemented or not at all implemented. This class contains one or more abstract methods which are simply method declarations without a body.


12. When Abstract methods are used?
If you want a class to contain a particular method but you want the actual implementation of that method to be determined by child classes, you can declare the method in the parent class as abstract.


13.  What is Encapsulation?
It is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. Therefore encapsulation is also referred to as data hiding.


14. What is the primary benefit of Encapsulation?
The main benefit of encapsulation is the ability to modify our implemented code without breaking the code of others who use our code. With this Encapsulation gives maintainability, flexibility and extensibility to our code.


15. What is data hiding?
Data Hiding is a concept in Java. We got the luxary of hiding data using e different access levels - private, protected, public using Java Programming Language.

Private members and functions can only be accessed by class members of the class. You can think the data abstraction is also using data hiding techniques. Protected members can be accessed only by the class and its derived classes.


16. What is an applet?
An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java application because it has the entire Java API at its disposal.


17.  An applet extend which class?
An applet extends java.applet.Applet class.


18.  What is function overloading?
If a class has multiple functions by same name but different parameters, it is known as Method Overloading.


19.  What is function overriding?
If a subclass provides a specific implementation of a method that is already provided by its parent class, it is known as Method Overriding.


20. Explain the basic features of OOPs.
The following are the four basic features of OOP:
·         Object
·         Class
  • Abstraction - Refers to the process of exposing only the relevant and essential data to the users without showing unnecessary information.
  • Polymorphism - Allows you to use an entity in multiple forms.
  • Encapsulation - Prevents the data from unwanted access by binding of code and data in a single unit called object.
  • Inheritance - Promotes the reusability of code and eliminates the use of redundant code. It is the property through which a child class obtains all the features defined in its parent class. When a class inherits the common properties of another class, the class inheriting the properties is called a derived class and the class that allows inheritance of its common properties is called a base class.

21. Explain the basic features of Java Programming Language.
Simple
  Looks familiar to existing programmers: related to C and C++:
  Omits many rarely used, poorly understood, confusing features of C++, like operator overloading, multiple inheritance, automatic coercions, etc.
  Contains no goto statement, but break and continue
  Has no header files and eliminated C preprocessor
  Eliminates much redundancy (e.g. no structs, unions, or functions)
  has no pointers
Java is an object-oriented language, which means that you focus on the data in your application and methods that manipulate that data, rather than thinking strictly in terms of procedures.
In an object-oriented system, a class is a collection of data and methods that operate on that data. Taken together, the data and methods describe the state and behavior of an object. Classes are arranged in a hierarchy, so that a subclass can inherit behavior from its superclass.
Java comes with an extensive set of classes, arranged in packages, that you can use in your programs.
  It has a spring-like transparent RPC system
  Now uses mostly tcp-ip based protocols like ftp & http
Java supports various levels of network connectivity through classes in the java.net package (e.g. the URL class allows a Java application to open and access remote objects on the internet).
The Java compiler generates byte-codes, rather than native machine code. To actually run a Java program, you use the Java interpreter to execute the compiled byte-codes. Java byte-codes provide an architecture-neutral object file format. The code is designed to transport programs efficiently to multiple platforms.
  rapid turn-around development
  Software author is protected, since binary byte streams are downloaded and not the source code
Java has been designed for writing highly reliable or robust software:
  language restrictions (e.g. no pointer arithmetic and real arrays) to make it impossible for applications to smash memory (e.g overwriting memory and corrupting data)
  Java does automatic garbage collection, which prevents memory leaks
  extensive compile-time checking so bugs can be found early; this is repeated at runtime for flexibilty and to check consistency
Security is an important concern, since Java is meant to be used in networked environments. Without some assurance of security, you certainly wouldn't want to download an applet from a random site on the net and let it run on your computer. Java's memory allocation model is one of its main defenses against malicious code (e.g can't cast integers to pointers, so can't forge access). Furthermore:
  access restrictions are enforced (public, private)
  byte codes are verified, which copes with the threat of a hostile compiler
  compiler generates bytecodes, which have nothing to do with a particular computer architecture
  easy to interpret on any machine
Java goes further than just being architecture-neutral:
  no "implementation dependent" notes in the spec (arithmetic and evaluation order)
  standard libraries hide system differences
  the Java environment itself is also portable: the portability boundary is POSIX compliant
Java is an interpreted language, so it will never be as fast as a compiled language as C or C++. In fact, it is about 20 times as slow as C. However, this speed is more than enough to run interactive, GUI and network-based applications, where the application is often idle, waiting for the user to do something, or waiting for data from the network.
Java allows multiple concurrent threads of execution to be active at once. This means that you could be listening to an audio clip while scrolling the page and in the background downloading an image. Java contains sophisticated synchronization primitives (monitors and condition variables), that are integrated into the language to make them easy to use and robust. The java.lang package provides a Thread class that supports methods to start, run, and stop a thread, and check on its status.
Java was designed to adapt to an evolving environment:
  Even after binaries have been released, they can adapt to a changing environment
  Java loads in classes as they are needed, even from across the network
  It defers many decisions (like object layout) to runtime, which solves many of the version problems that C++ has
  Dynamic linking is the only kind there is



Comments

Popular posts from this blog

IT resources @ DAIICT

I blinked and the day passed ...

The world is what it is