Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Programming: Classes, Methods, and Exceptions, Study Guides, Projects, Research of Computer Science

Answers to various questions about Java programming, including encapsulation, JVM, method overloading, inheritance, access modifiers, exceptions, and event handling. It also covers topics such as the Panel and Applet classes, packages, constructors, and coding standards.

Typology: Study Guides, Projects, Research

2021/2022

Uploaded on 12/24/2022

sreeja-bobbili
sreeja-bobbili 🇮🇳

1 document

1 / 15

Toggle sidebar

Related documents


Partial preview of the text

Download Java Programming: Classes, Methods, and Exceptions and more Study Guides, Projects, Research Computer Science in PDF only on Docsity! Top 100 Java Interview Questions & Answers 1. What is a class in Java? Answer: Java encapsulates the codes in various classes which define new data types. These new data types are used to create objects. 2. What is a JVM? Answer: JVM is Java Virtual Machine which is a run time environment for the compiled java class files. 3. What is the right data type to represent a price in Java? Answer: BigDecimal, if memory is not a concern and Performance, is not critical, otherwise double with predefined precision. 4. Does Java support multiple inheritances? Answer: Java doesn’t support multiple inheritances. 5. What are the supported platforms by Java Programming Language? Answer: Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX/Linux like HP-Unix, Sun Solaris, Red hat Linux, Ubuntu, Cent OS, etc. 6. List any five features of Java? Answer: Some features include Object Oriented Platform Independent Robust Interpreted Multi-threaded 7. Explain method overloading? Answer: When a Java program contains more than one methods with the same name but different properties, then it is called method overloading. 8. What restrictions are placed on the location of a package statement within a source code file? Answer: A package statement must appear as the first line in a source code file (eliminating blank lines and comments). 9. What method is used to specify a container’s layout? Answer: The setLayout() method is used to specify a container’s layout. 10. What is the immediate superclass of the Applet class? Answer: The Panel class is the immediate superclass of the Applet class. 11. What are the access modifiers in Java? Answer: There are 3 access modifiers. Public, protected and private, and the default one if no identifier is specified is called friendly, but programmer cannot specify the friendly identifier explicitly. 12. What is are packages? Answer: A package is a collection of related classes and interfaces providing access protection and namespace management. 13. What is meant by Inheritance and What are its advantages? Answer: Inheritance is the process of inheriting all the features from a class. The advantages of inheritance are reusability of code and accessibility of variables and methods of the super class by subclasses. 14. Can we rethrow the same exception from catch handler? Answer: Yes, we can rethrow the same exception from our catch handler. If we want to rethrow checked exception from a catch block we need to declare that exception. 15. what value is a variable of the String type automatically initialized? Answer: The default value of a String type is null. 16. When a thread blocks on I/O, what state does it enter? Answer: When it blocks on I/O, A thread enters the waiting state. 17. Which containers use a Flow Layout as their default layout? Answer: The Panel and Applet classes use the Flow Layout as their default layout. 18. Explain Java Coding Standards for Constants ? Answer: Constants in java are created using static and final keywords. 34. What is the difference between this() and super()? Answer: this() can be used to invoke a constructor of the same class whereas super() can be used to invoke a super class constructor. 35. What is a native method? Answer: A native method is a method that is applied in a language other than Java. 36. What value does read Line() return when it has reached the end of a file? Answer:The readLine() method returns null when it has reached the end of a file. 37. What is the Java API? Answer: The Java API is a large collection of ready-made software components that provide many usefulcapabilities, such as graphical user interface (GUI) widgets. 38. Why there are no global variables in Java? Answer: Global variables are globally accessible. Java does not support globally accessible variables due to following reasons: The global variables breaks the referential transparency Global variables creates collisions in namespace. 39. What are different types of access modifiers? Answer: public: Any thing declared as public can be accessed from anywhere. private: Any thing declared asprivate can’t be seen outside of its class. protected: Any thing declared as protected can be accessedby classes in the same package and subclasses in the other packages. default modifier : Can beaccessed only to classes in the same package. 40. What is Constructor? Answer: A constructor is a special method whose task is to initialize the object of its class. It is special because its name is the same as the class name. They do not have return types, not even void and therefore they cannot return values. They cannot be inherited, though a derived class can call the base class constructor. Constructor is invoked whenever an object of its associated class is created. 41. What is an Iterator ? Answer: The Iterator interface is used to step through the elements of a Collection. Iterators let you process each element of a Collection. Iterators are a generic way to go through all the elements of a Collection no matter Define How it is organized. Iterator is an Interface implemented a different way for every Collection. 42. What is the difference between Reader/Writer and InputStream/Output Stream? Answer: The Reader/Writer class is character-oriented and the InputStream/OutputStream class is byte- oriented. 43. What is servlet? Answer: Servlets are modules that extend request/response-oriented servers, such as java-enabled webservers. For example, a servlet might be responsible for taking data in an HTML order-entry form and applying the business logic used to update a company’s order database. 44. What is clipping? Answer: Clipping is the process of confining paint operations to a limited area or shape. 45. What is memory leak? A memory leak is where an unreferenced object that will never be used again still hangs around in memory and doesnt get garbage collected. 46. Can a for statement loop indefinitely? Answer: Yes, a for statement can loop indefinitely. For example, consider the following: for(;;) 47. Explain Java Coding standards for Methods? Answer: 1) Method names should start with small letters. 2) Method names are usually verbs 3) If a method contains multiple words, every inner word should start with an uppercase letter. Ex : toString() 4) Method name must be combination of verb and noun Ex : getCarName(),getCarNumber() 48. Why Java is not a pure Object Oriented language? Answer: Java supports primitive types such as int, byte, short, long, etc that why it is not said to be a pure object-oriented language. 49. What are the access modifiers? Answer: Java provides three access controls such as public, private and protected access modifier. When none of these are used, it’s called default access modifier. 50. Can we overload the main method? Answer: Yes, we can overload the main method with syntax as public static void main(String args[]). 51. What is the method in java? Answer: It contains the executable body that can be applied to the specific object of the class. The method includes method name, parameters or arguments and return type and a body of executable code. Syntax : type methodName(Argument List){ ex: public float add(int a, int b, int c) methods can have multiple arguments. Separate with commas when we have multiple arguments. thrown in the method are instances of their subclass. 52. Explain about Automatic type conversion in java? Answer: Java automatic type conversion is done if the following conditions are met: 1) When two types are compatible Ex: int, float int can be assigned directly to float variable. 2) Destination type is larger than source type. Ex: int, long. Int can be assigned directly to long .Automatic type conversion takes place if int is assigned to long because long is larger datatype than int. Widening Conversion comes under Automatic type conversion. 53. What is the difference between the prefix and postfix forms of the ++ operator? Answer: The prefix form performs the increment operation and returns the value of the increment operation. The postfix form returns the current value all of the expression and then performs the increment operation on that value. 68. How can we find the actual size of an object on the heap? Answer: In Java, there is no way to find out the actual size of an object on the heap. 69. Can a variable be local and static at the same time? Answer: No a variable can’t be static as well as local at the same time. Defining a local variable as static gives compilation error. 70. Can we have static methods in an Interface? Answer: Static methods can’t be overridden in any class while any methods in an interface are by default abstract and are supposed to be implemented in the classes being implementing the interface. So it makes no sense to have static methods in an interface in Java. 71. In how many ways we can do synchronization in java? Answer: There are two ways to do synchronization in java: 1) Synchronized methods 2) Synchronized blocks To do synchronization we use the synchronized keyword. 72. When do we use synchronized blocks and advantages of using synchronized blocks? Answer: If very few lines of code require synchronization then it is recommended to use synchronized blocks. The main advantage of synchronized blocks over synchronized methods is it reduces the waiting time of threads and improves performance of the system. 73. What is the difference between access specifiers and access modifiers in java? Answer: In C++ we have access specifiers as public, private, protected and default and access modifiers as static, final. But there is no such division of access specifiers and access modifiers in java. In Java, we have access to modifiers and nonaccess modifiers. Access Modifiers: public, private, protected, default Non Access Modifiers: abstract, final, strip. 74. Define How objects are stored in Java? Answer: In java, each object when created gets a memory space from a heap. When an object is destroyed by a garbage collector, the space allocated to it from the heap is re-allocated to the heap and becomes available for any new objects. 75. What access modifiers can be used for class? Answer: We can use only two access modifiers for class public and default. public: A class with a public modifier can be visible 1) In the same class 2) In the same package subclass 3) In the same package nonsubclass 4) In the different package subclass 5) In the different package nonsubclass. default: A class with default modifier can be accessed 1) In the same class 2) In the same package subclass 3) In the same package nonsubclass 4) In the different package subclass 5) In the different package nonsubclass. ( ) 76. Explain about abstract classes in java? Answer: Sometimes we may come across a situation where we cannot provide implementation to all the methods in a class. We want to leave the implementation to a class that extends it. In such a case, we declare a class as abstract. To make a class abstract we use keyword abstract. Any class that contains one or more abstract methods is declared as abstract. If we don’t declare a class as abstract which contains abstract methods we get a compile-time error. We get the following error. “The type must be an abstract class to define abstract methods.” Signature; abstract class. For example, if we take a vehicle class we cannot provide implementation to it because there may be two-wheelers, four-wheelers, etc. At that moment we make vehicle class abstract. All the common features of vehicles are declared as abstract methods in vehicle class. Any class which extends the vehicle will provide its method implementation. It’s the responsibility of subclass to provide the implementation. The important features of abstract classes are: 1) Abstract classes cannot be instantiated. 2) An abstract class contains abstract methods, concrete methods or both. 3) Any class which extends abstract class must override all methods of an abstract class. 4) An abstract class can contain either 0 or more abstract methods. Though we cannot instantiate abstract classes we can create object references. Through superclass references, we can point to subclass. 77. Can we create a constructor in abstract class? Answer: We can create a constructor in the abstract class, it doesn’t give any compilation error. But when we cannot instantiate class there is no use in creating a constructor for abstract class. 78. String and StringBuffer both represent String objects. Can we compare String andStringBuffer in Java? Answer: Although String and StringBuffer both represent String objects, we can’t compare them with each other and if we try to compare them, we get an error. 79. In how many ways we can create threads in java? Answer: We can create threads in java by any of the two ways : 1) By extending Thread class 2) By implementing the Runnable interface. 80. Explain creating threads by implementing Runnable class? Answer: This is the first and foremost way to create threads. By implementing the runnable interface and implementing the run() method we can create a new thread. Method signature : public void run() Run is the starting point for execution for another thread within our program. Example : public class MyClass implements Runnable { @Override public void run() 81. When do we use synchronized methods in java? Answer: If multiple threads try to access a method where the method can manipulate the state of the object, in such a scenario we can declare a method as synchronized. 82. Can we cast any other type to Boolean Type with type casting? Answer: No, we can neither cast any other primitive type to Boolean data type nor can cast Boolean data typeto any other primitive data type. 83. What are synchronized methods and synchronized statements?