{"id":9358,"date":"2018-02-22T10:18:10","date_gmt":"2018-02-22T10:18:10","guid":{"rendered":"https:\/\/www.fita.in\/?p=9358"},"modified":"2025-10-15T06:51:16","modified_gmt":"2025-10-15T06:51:16","slug":"java-interview-questions-freshers","status":"publish","type":"post","link":"https:\/\/www.fita.in\/java-interview-questions-freshers\/","title":{"rendered":"111 Important Java Interview Questions for Freshers"},"content":{"rendered":"
Welcome to the world of Java! Java is both a platform and a programming language. It’s one of the most popular programming languages, creating numerous job<\/a> opportunities. This blog discusses some critical Java interview questions and answers for freshers, frequently asked in interviews, to help them answer more effectively.<\/p>\r\n In Java, the keywords placed before a class name that define its accessibility are known as access modifiers.<\/p>\r\n Master Java and become job-ready for top tech companies. <\/strong><\/p>\r\n Start coding your future now with FITA Academy<\/strong><\/p>\r\n Enrol Now<\/strong><\/a>\r\n <\/div>\r\n<\/section>\r\n A subclass, a derived class, inherits from a superclass. It has access to every superclass’s protected and open fields and methods.<\/p>\r\n Sample code demonstrating an inner class and subclass relationship:<\/strong><\/p>\r\n This is frequently asked in most Core Java<\/a> interview questions for freshers. Data encapsulation is a fundamental principle in object-oriented programming that involves bundling data (attributes) and the methods (operations) that interact with that data into a single unit, known as an object. This encapsulation ensures data security and modularity.<\/p>\r\n Encapsulating data within an object allows you to control access through well-defined methods. This method prevents unauthorized access or modification, protecting the integrity of your data and is called data hiding.<\/p>\r\n Sample code demonstrating data encapsulation:<\/strong><\/p>\r\n This is one of the frequently asked Java questions for freshers interview. It’s essential to understand static variables and static methods. Many successful candidates, especially those who completed a comprehensive Java course in Erode<\/a>, emphasize this concept. They share a variable or a method instead of making unique copies for every item, meaning they divide one object of a class across several others.<\/p>\r\n By declaring a method or variable as static, it becomes shared and can be accessed directly through the class itself rather than through an instance. This allows for efficient memory utilisation and enables convenient access to common resources or operations that are not specific to individual object instances.<\/p>\r\n This is one of the must-know core Java interview questions for freshers. Loops in Java are used to execute a statement or a block of statements repeatedly. There are 3 types of loops in Java:<\/p>\r\n For Loops:<\/strong><\/p>\r\n For loops execute statements a specified number of times. They are commonly used when the number of iterations is known in advance.<\/p>\r\n While Loops:<\/strong><\/p>\r\n When it is necessary to execute statements repeatedly, While loops are used until a specific condition is fulfilled. This type of condition is evaluated before the execution of the statements.<\/p>\r\n Do-While Loops:<\/strong><\/p>\r\n Do-while loops are similar to while loops but with one difference: the condition is evaluated after the execution of the statement block. By doing this, even if the condition is initially false, it is ensured that the assertions are carried out at least once.<\/p>\r\n Java interview questions for freshers with answers are not complete without this question. Let us get into it in detail. In Java, a singleton class can have only one instance, which owns all of its variables and methods. The singleton class pattern is used when the number of objects created for a class needs to be restricted.<\/p>\r\n An example of using a singleton is when there is a requirement to have a single connection to a database only because of driver or licensing restrictions.<\/p>\r\n Java programming questions for freshers include this question too. The “break” and “continue” statements are key concepts used in loops.<\/p>\r\n When a loop is discovered, the “break” command is used to end it instantly. It completely exits the loop, regardless of the loop condition. For example:<\/p>\r\n Using these statements provides control over loop execution, allowing specific conditions to be met to terminate or skip iterations. <\/p>\r\n An infinite loop is a loop which runs indefinitely without any condition to terminate it. To break out of an infinite loop, a breaking logic or condition must be included within the loop’s statement block.<\/p>\r\n An infinite loop can be declared as follows:<\/p>\r\n Constant declaration in Java requires the usage of the “final” keyword. A final variable’s value cannot be altered once it has been assigned.<\/p>\r\n Here’s an example of declaring a constant named const_val:<\/p>\r\n private final int const_val =100;<\/p>\r\n Additionally, the “final” keyword has other uses in Java. When a method is marked as final, subclasses cannot override it. Final methods are resolved at compile time, making them faster than other methods.<\/p>\r\n “Final” is one among the important Java concepts for interview. So, don’t skip it.<\/p>\r\n In Java, the float and double variables differ in precision and memory size.<\/p>\r\n The float variable represents a single-precision floating-point decimal number and typically occupies 4 bytes of memory. It offers less precision compared to double.<\/p>\r\n On the other hand, the double variable represents a double-precision decimal number and usually occupies 8 bytes of memory. Compared to float, it provides higher precision and a wider range of values. Java basic questions for freshers include this one for sure.<\/p>\r\n Imp questions in Java have this on the list. In Java, there are several methods for producing random numbers. One approach is by using the Math.random() method, which generates random double values between 0.0 (inclusive) and 1.0 (exclusive). To obtain random numbers within a specific range, you can multiply the result of Math.random() by the desired range and add an offset value.<\/p>\r\n double randomValue = Math.random();\/\/ Generates random value between 0.0 and 1.0<\/p>\r\n double scaledValue = randomValue *(max – min)+ min;\/\/ Generates random value within a specific range<\/p>\r\n The conditional operator, commonly called the ternary operator, is used to make decisions and assign a value to a variable based on the evaluation of a Boolean expression.<\/p>\r\n The ternary operator is a concise way to make decisions and assign values based on conditions in Java.<\/p>\r\n The Java foundation class, from which all other classes are descended, is java.lang.Object.<\/p>\r\n The default case is used in a switch statement when none of the other switch conditions match. It acts as an optional case that is executed when no other cases are satisfied.<\/p>\r\n The default case should be placed after all the other cases have been coded within the switch statement.<\/p>\r\n The default case provides a fallback option when none of the other cases match in a switch statement.<\/p>\r\n Every Java interview questions for freshers pdf contain this question. <\/p>\r\n Packages are a way to organise code in Java and group related classes, interfaces, and other code elements together. They allow developers to modularise their code, making it easier to manage and reuse.<\/p>\r\n Packages contain a logically related collection of classes and interfaces. By organising code into packages, developers can improve code organisation and maintainability.<\/p>\r\n One significant benefit of using packages is the ability to control access to classes and encapsulate code within a specific namespace. This helps prevent naming conflicts and provides a clear boundary for accessing code from other packages.<\/p>\r\n Packages also facilitate code reuse by allowing the importation of classes and resources from other packages. Once code is packaged in a particular package, it can be easily imported into other classes and used without duplicating the code.<\/p>\r\n Power up your career with FITA Academy’s Java course – Code once, run everywhere!<\/p>\r\n Enrol Now<\/a>\r\n <\/div>\r\n<\/section>\r\n The main() method in Java does not return any data. As a result, a void return type is always stated when it is used. This is a simple yet tricky question on your learning on Java for interview preparation purposes.<\/p>\r\n The following is the main difference in Java, between an abstract class and an interface:<\/p>\r\n Interface<\/strong><\/p>\r\n An interface can only declare public static method signatures without providing their concrete implementation. It serves as a contract for classes that implement it, ensuring they define the specified methods. Interfaces can also declare constants and can be implemented by multiple classes.<\/p>\r\n Abstract Class<\/strong><\/p>\r\n An abstract class can have members with various access specifiers (private, public, etc.) and can include abstract and concrete methods. It can provide a partial implementation of methods. Abstract classes are designed to be extended by subclasses, which are responsible for implementing abstract methods defined by the abstract class. Your Java technical interview questions for freshers never miss this one.<\/p>\r\n Yes, it is possible to use the abstract keyword before the class name to create an abstract class, even if it doesn’t contain any abstract methods. However, if a class contains at least one abstract method, it must be declared as an abstract class, otherwise it will result in an error.<\/p>\r\n No, when a package is imported in Java, its sub-packages are not automatically imported. If the developer wants to use classes from the sub-packages, they need to import them separately.<\/p>\r\n For example, if a developer imports the package university.*, it will load all the classes from the university package, but it will not load classes from its sub-packages. To access classes from a sub-package, the developer needs to import them like this explicitly:<\/p>\r\n import university.department.*;<\/p>\r\n This is one of the Java basic questions for freshers to know before attending an interview.<\/p>\r\n Interfaces are generally considered to have slightly slower performance than abstract classes due to the additional indirections required for interfaces. Another important consideration for developers is that a class can only extend a single abstract class while it can implement multiple interfaces.<\/p>\r\n Using interfaces also imposes an additional responsibility on developers. When a class implements an interface, the developer is obligated to implement all the methods defined in that interface.<\/p>\r\n Object Oriented Programming (OOP) Concepts<\/strong><\/p>\r\n This section will fundamentally delve into OOP concepts that often come up in the Java interview questions and answers for freshers.<\/p>\r\n In Java, passing arguments to a function by reference is impossible. Java only supports passing arguments by value, where a copy of the value is passed to the function. This means the original variable outside of the function is unaffected by changes made to the parameter inside the function.<\/p>\r\n In contrast to other programming languages, such as C++, Java does not provide direct support for passing arguments by reference.<\/p>\r\n No, the main method should be declared public static to run applications correctly in Java. If the main method is declared as private, it will compile without any errors. However, it will not be executed and will result in a runtime error.<\/p>\r\n Serialisation is commonly used when data needs to be transmitted over a network. By using serialisation, an object’s state can be converted and saved into a byte stream. This byte stream can then be transferred through the network, and the object can be reconstructed at the destination. Serialisation is particularly useful when working with distributed systems, client-server communication, or persistent object storage.<\/p>\r\n This is one of the core Java interview questions for fresher, while preparing for an interview.<\/p>\r\n In Java, an object can be serialised by implementing the Serializable interface. By implementing this interface, the class can convert its objects into a byte stream through serialization. When a class implements the Serialisable interface, most of its objects can be serialised, and their state can be stored in the byte stream. This allows the object to be easily transmitted or stored persistently.<\/p>\r\n Your core Java fresher interview questions always tag this question. So, focus on it.<\/p>\r\n In Java, the finally block is always carried out regardless of whether an exception occurs or not. It is not possible to directly skip the finally block based on an exception in the catch block.<\/p>\r\n However, if you want to terminate the program forcefully and avoid executing any statements in the final block, you can use the System.exit(0) code line at the end of the try block. This will abruptly terminate the program and prevent the execution of subsequent code, including the final block. It’s important to note that using System.exit(0) should be done cautiously, as it terminates the entire Java program abruptly.<\/p>\r\n One of the most java questions answers for freshers is whether a Try block must always be followed by a Catch block. In Java, a Try block must be followed by either a Catch block, a Finally block, or both. Whenever an exception is raised inside the Try block, it must be caught and handled in the Catch block. Alternatively, specific tasks that need to be executed before the termination of the code can be placed in the Finally block.<\/p>\r\n The Catch block allows for catching and handling specific exceptions, allowing one to handle exceptions gracefully or perform error logging. Code that must always be run is specified regardless of whether an exception happens or not, in the Finally block. It is typically used for tasks such as closing resources or releasing acquired locks.<\/p>\r\n Yes, a class may have several constructors with various inputs. The constructor used for object creation depends on the arguments passed during the object’s instantiation. This allows flexibility in creating objects with different initializations based on the constructor used.<\/p>\r\n With deep knowledge on Java coding interview questions and answers, you can upgrade your coding skills in Java and succeed in your interviews.<\/p>\r\n The class constructor is invoked every time an object is created using the new keyword.<\/p>\r\n For example, in the following class, the constructor is invoked twice when two objects are created using the new keyword:<\/p>\r\n Output:<\/strong><\/p>\r\n Displaying from subclass<\/p>\r\n Printing from superclass<\/p>\r\n Explanation:<\/strong><\/p>\r\n The main method is invoked, creating an object of the Subclass class. The displayResult method is then called on this object. The output will first display “Displaying from subclass” as it is defined in the Subclass class. After that, the super.displayResult() line calls the displayResult method of the superclass Superclass, resulting in the output “Printing from superclass” being displayed.<\/p>\r\n Java programming questions and answers contains this type of coding quizzes also.<\/p>\r\n No, static methods cannot be overridden. Static methods belong to the class rather than individual objects and are resolved at compile time. We will not encounter a compilation error when we try to replace a static method. However, the overriding will not have any effect when executing the code.<\/p>\r\n To enhance your programming skills, Java Training in Pondicherry<\/a> offers specialized courses and expert instruction tailored to your career objectives.<\/p>\r\n String s1 =”I am Java Expert”;<\/p>\r\n String s2 =”I am C Expert”;<\/p>\r\n String s3 =”I am Java Expert”;<\/p>\r\n Answer: Two objects of the java.lang.String class have been created in the above example. Both s1 and s3 reference the same object, which contains the text “I am Java Expert”.<\/p>\r\n No, a string is not classified as a primitive data type in Java. When a string is created, it is actually an object of the java.lang.String class that is created. Once a string object is created, various built-in methods of the String class can be used on the string object.<\/p>\r\n Java questions for freshers interview include this important question also.<\/p>\r\n An array collects information of a static, same primitive type, but vectors are believed to be dynamic in nature and hold the data of multiple data types.<\/p>\r\n Strings are immutable objects in Java, which means that once a value is assigned to a string, it cannot be changed. If any modifications are made to a string, a new string object is created with the updated value, while the original string object remains unchanged.The integrity and security of Java’s string objects are guaranteed by their immutability.<\/p>\r\n Multi-threading is a fundamental programming concept that involves concurrently running many tasks in the same program. The same process stack is shared by all running threads, enabling improved program performance. Multi-threading is an important topic frequently asked in Java questions for freshers.<\/p>\r\n The Runnable interface is used in Java to facilitate the execution of multi-threaded applications. The java.lang.Runnable interface is created by a class to facilitate the functionality of multi-threading.<\/p>\r\n Unlike Strings, which are immutable, string buffers are dynamic in nature and allow us to change the values of StringBuffer objects. Therefore, it is always a good choice to use StringBuffer when data needs to be changed frequently. When doing so, use a String., Each data modification will result in the creation of a fresh String object, resulting in additional overhead.<\/p>\r\n Java offers two different ways to construct multi-threaded applications:<\/p>\r\n Implementing the java.lang.Runnable interface:<\/strong> Classes can implement this interface to enable multi-threading. The interface includes a run() method that is executed when the thread starts.<\/p>\r\n Extending the java.lang.Thread class:<\/strong> Multi-threading can be achieved by creating a class that extends the Thread class. The class can override the run() method to specify the programme that will run when the thread begins.<\/p>\r\n It is one of the crucial core Java interview questions for experience faced during an interview.<\/p>\r\n In Java, garbage collection occurs automatically when an object is no longer referenced. The object is then automatically removed from memory. Java triggers garbage collection through the Runtime.gc() method or the System.gc() method.<\/p>\r\n To make sure that the code exits the switch block following the execution of the valid case, the “break” statement is used after each case in a switch statement (except from the final case). Without the break statement, the code would continue executing the subsequent cases, resulting in incorrect outcomes. The execution flow is controlled and limited to the desired scenario by utilizing the break statement.<\/p>\r\n It is one of the core Java fresher interview questions that are mostly repeated.<\/p>\r\n No, the constructor of a class can only be called once for an object. It is automatically invoked during the creation of the object using the `new` keyword, and after an object has been constructed, we cannot directly call its constructor again.<\/p>\r\n This question appears in Java interview questions for 10 years experience also. Consider its importance and be prepared with the answers.<\/p>\r\n A class’s private members cannot be accessed from outside of that class, and this includes other classes in the same package. Therefore, an object of Class B cannot access the private members of Class A.<\/p>\r\n Yes, Two methods with the same name but distinct parameters might exist in the same class. The called procedure is determined by the parameters passed when calling the method.<\/p>\r\n For example, in the following class, there are two print methods with the same name but different parameters. The appropriate method will be called depending on the parameters passed:<\/p>\r\n The idea of cloning can be used to duplicate an object. By implementing the Cloneable interface and utilising the clone() method, we can create copies of objects that retain their current state.<\/p>\r\n The primary advantage of using inheritance is code reusability, as it enables subclasses to inherit and reuse code from their superclass. Additionally, inheritance enables polymorphism, allowing new functionality to be introduced without impacting existing derived classes.<\/p>\r\n Here’s a sample program demonstrating inheritance:<\/p>\r\n The class’s standard access specifier for variables and methods is “package-protected” or “default.” This means that the variables and methods are accessible within the same package, but not outside of it.<\/p>\r\n In Java, the concept of pointers is not directly available as in some other programming languages, such as C or C++. Java utilizes references instead of pointers to handle objects. References in Java provide a safe and simplified way to access objects without the need for explicit memory management. Questions on Java for interview preparation include this one, too. <\/p>\r\n If we want to prevent a class from being further extended by any other class, prior to the class name, we can use the final keyword.<\/p>\r\n In the following example, the Stone class is declared as final, which means it cannot be subclassed:<\/p>\r\n public final classStone{<\/p>\r\n \/\/ Class methods and variables<\/p>\r\n }<\/p>\r\n By making a class final, we ensure that any other class cannot extend or inherit it. This provides control over the class’s implementation and behavior, preventing any modifications or extensions.<\/p>\r\nCore Java Concepts<\/strong><\/h2>\r\n
1. What various access specifiers are there for Java classes?<\/h3>\r\n
\r\n
\r\n
2. What is the difference between a subclass and an inner class?<\/h3>\r\n
publicclassOuterClass{\r\n\r\nprivate int outerVariable;\r\n\r\npublicvoidouterMethod(){\r\n\r\nSystem.out.println(\"This is the outer method\");\r\n\r\n}\r\n\r\npublicclassInnerClass{\r\n\r\npublicvoidinnerMethod(){\r\n\r\nouterVariable =10;\/\/ Accessing the outer variable\r\n\r\nouterMethod();\/\/ Accessing the outer method\r\n\r\nSystem.out.println(\"This is the inner method\");\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n\r\npublicclassSubClassextendsSuperClass{\r\n\r\npublicvoidsubClassMethod(){\r\n\r\nprotectedField =20;\/\/ Accessing the protected field of the superclass\r\n\r\npublicMethod();\/\/ Accessing the public method of the superclass\r\n\r\nSystem.out.println(\"This is the subclass method\");\r\n\r\n}\r\n\r\n}\r\n\r\n\/\/ Usage of the inner class and subclass\r\n\r\nOuterClass outerObject =newOuterClass();\r\n\r\nOuterClass.InnerClass innerObject = outerObject.new InnerClass();\r\n\r\ninnerObject.innerMethod();\r\n\r\nSubClass subObject =newSubClass();\r\n\r\nsubObject.subClassMethod();\r\n<\/code><\/pre>\r\n<\/div>\r\n3. What is data encapsulation?<\/h3>\r\n
\r\npublicclassPerson{\r\n\r\nprivate String name;\r\n\r\nprivate int age;\r\n\r\npublicvoidsetName(String newName){\r\n\r\nname = newName;\r\n\r\n}\r\n\r\npublic String getName(){\r\n\r\nreturn name;\r\n\r\n}\r\n\r\npublicvoidsetAge(int newAge){\r\n\r\nage = newAge;\r\n\r\n}\r\n\r\npublic int getAge(){\r\n\r\nreturn age;\r\n\r\n}\r\n\r\n}\r\n\r\npublicclassMain{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\nPerson person =newPerson();\r\n\r\nperson.setName(\"John\");\r\n\r\nperson.setAge(25);\r\n\r\nSystem.out.println(\"Name: \"+ person.getName());\r\n\r\nSystem.out.println(\"Age: \"+ person.getAge());\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n4. What do static methods and variables accomplish?<\/h3>\r\n
5. What do Java loops do? Which three types of loops are there?<\/h3>\r\n
6. What is a Singleton Class? Can you provide an example?<\/h3>\r\n
\r\npublicclassDatabaseConnection{\r\n\r\nprivatestatic DatabaseConnection instance;\r\n\r\nprivateDatabaseConnection(){\r\n\r\n\/\/ Private constructor to prevent direct instantiation\r\n\r\n}\r\n\r\npublicstatic DatabaseConnection getInstance(){\r\n\r\nif(instance ==null){\r\n\r\ninstance =newDatabaseConnection();\r\n\r\n}\r\n\r\nreturn instance;\r\n\r\n}\r\n\r\npublicvoidconnect(){\r\n\r\nSystem.out.println(\"Connected to the database\");\r\n\r\n}\r\n\r\n}\r\n\r\npublicclassMain{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\nDatabaseConnection connection = DatabaseConnection.getInstance();\r\n\r\nconnection.connect();\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n7. Describe the distinctions between the “break” and “continue” statements<\/h3>\r\n
\r\nfor(int counter =0; counter <10; counter++){\r\n\r\nSystem.out.println(counter);\r\n\r\nif(counter ==4){\r\n\r\nbreak;\r\n\r\n}\r\n\r\n}\r\n\r\nThe \u201ccontinue\u201d statement, on the other hand, is utilised to skip the current iteration of a loop and move on to the subsequent iteration. It allows the loop to continue its execution. For example:\r\n\r\nfor(int counter =0; counter <10; counter++){\r\n\r\nSystem.out.println(counter);\r\n\r\nif(counter ==4){\r\n\r\ncontinue;\r\n\r\n}\r\n\r\nSystem.out.println(\"This will not be printed when counter is 4\");\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n8. What is an infinite loop? How is an infinite loop declared?<\/h3>\r\n
\r\nfor(;;){\r\n\r\n\/\/ Statements to execute\r\n\r\n\/\/ Add any loop-breaking logic\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n9. What is the “final” keyword in Java used for? Can you provide an example?<\/h3>\r\n
10. What are the Java float and double variables differences?<\/h3>\r\n
11. How can you generate random numbers in Java?<\/h3>\r\n
12. What is a ternary operator? Can you provide an example?<\/h3>\r\n
\r\npublicclassConditionTest{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\nString status;\r\n\r\nint rank =3;\r\n\r\nstatus =(rank ==1)?\"Done\":\"Pending\";\r\n\r\nSystem.out.println(status);\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n13. What is the Java basic class that all other classes are descended from?<\/h3>\r\n
14. What is a default case in a switch statement? Can you provide an example?<\/h3>\r\n
\r\npublicclassSwitchExample{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\nint score =4;\r\n\r\nswitch(score){\r\n\r\ncase1:\r\n\r\nSystem.out.println(\"Score is 1\");\r\n\r\nbreak;\r\n\r\ncase2:\r\n\r\nSystem.out.println(\"Score is 2\");\r\n\r\nbreak;\r\n\r\ndefault:\r\n\r\nSystem.out.println(\"Default Case\");\r\n\r\n}\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n15. What are Java packages? What is the significance of packages?<\/h3>\r\n
16. Does the main() method in Java return any data?<\/h3>\r\n
17. Explain the difference between an Interface and an Abstract Class in Java.<\/h3>\r\n
18. Is it possible to declare a class as an Abstract class without having any abstract methods?<\/h3>\r\n
19. Does importing a package also import its sub-packages in Java?<\/h3>\r\n
20. What are the performance implications of interfaces compared to abstract classes?<\/h3>\r\n
21. Constant declaration in Java requires the usage of the “final” keyword. A final variable’s value cannot be altered once it has been assigned.<\/h3>\r\n
22. Is it possible to declare the main method of a class as private?<\/h3>\r\n
23. When should we use serialisation?<\/h3>\r\n
24. How does Java serialise an object?<\/h3>\r\n
25. Is there a way to skip the final block even if an exception occurs within the catch block?<\/h3>\r\n
26. Does Java’s Try block have to be followed by a Catch block in order to handle exceptions?<\/h3>\r\n
27. Can a class have multiple constructors?<\/h3>\r\n
28. When is the class constructor invoked?<\/h3>\r\n
\r\npublicclassConstExample{\r\n\r\nConstExample(){}\r\n\r\npublicstaticvoidmain(String args[]){\r\n\r\nConstExample c1 =newConstExample();\r\n\r\nConstExample c2 =newConstExample();\r\n\r\n}\r\n\r\n}\r\n\r\n<\/code><\/pre>\r\n<\/div>\r\n29. In the following example, what can be the output?<\/h3>\r\n
\r\npublicclassSuperclass{\r\n\r\npublicvoiddisplayResult(){\r\n\r\nSystem.out.println(\"Printing from superclass\");\r\n\r\n}\r\n\r\n}\r\n\r\npublicclassSubclassextendsSuperclass{\r\n\r\npublicvoiddisplayResult(){\r\n\r\nSystem.out.println(\"Displaying from subclass\");\r\n\r\nsuper.displayResult();\r\n\r\n}\r\n\r\npublicstaticvoidmain(String args[]){\r\n\r\nSubclass obj =newSubclass();\r\n\r\nobj.displayResult();\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n30. Is it possible to override static methods of a class?<\/h3>\r\n
Java String and Memory Management<\/strong><\/h2>\r\n
31. How many string objects can be produced in the following example?<\/h3>\r\n
32. Is a string considered a data type in Java?<\/h3>\r\n
33. What distinguishes a vector from an array?<\/h3>\r\n
\r\nimport java.util.Vector;\r\n\r\npublicclassVectorExample{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\n\/\/ Creating a vector\r\n\r\nVector names =newVector<>();\r\n\r\n\/\/ Adding elements to the vector\r\n\r\nnames.add(\"John\");\r\n\r\nnames.add(\"Alice\");\r\n\r\nnames.add(\"Bob\");\r\n\r\n\/\/ Accessing elements from the vector\r\n\r\nSystem.out.println(\"First name: \"+ names.get(0));\r\n\r\nSystem.out.println(\"Second name: \"+ names.get(1));\r\n\r\nSystem.out.println(\"Third name: \"+ names.get(2));\r\n\r\n\/\/ Changing an element in the vector\r\n\r\nnames.set(1,\"Eve\");\r\n\r\n\/\/ Removing an element from the vector\r\n\r\nnames.remove(0);\r\n\r\n\/\/ Printing all elements in the vector\r\n\r\nSystem.out.println(\"Elements in the vector: \"+ names);\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n34. Why are strings known as immutable in Java?<\/h3>\r\n
Java Multithreading<\/strong><\/h2>\r\n
35. What is Multi-Threading?<\/h3>\r\n
36. Why does Java employ the Runnable interface?<\/h3>\r\n
37. Which one should be used first when several changes to the data are required? String or StringBuffer?<\/h3>\r\n
38. What are the two ways that Java implements multi-threading?<\/h3>\r\n
\r\nclassMyRunnableimplementsRunnable{\r\n\r\npublicvoidrun(){\r\n\r\nSystem.out.println(\"Thread is running using Runnable interface.\");\r\n\r\npublicclassRunnableExample{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\nMyRunnable myRunnable =newMyRunnable();\r\n\r\nThread thread =newThread(myRunnable);\r\n\r\nthread.start();\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n\r\nclassMyThreadextendsThread{\r\n\r\npublicvoidrun(){\r\n\r\nSystem.out.println(\"Thread is running by extending Thread class.\");\r\n\r\n}\r\n\r\n}\r\n\r\npublicclassThreadExample{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\nMyThread myThread =newMyThread();\r\n\r\nmyThread.start();\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n39. How is garbage collection performed in Java?<\/h3>\r\n
Java Control Flow Statements<\/strong><\/h2>\r\n
40. Why is the “break” statement used each time a switch statement is used?<\/h3>\r\n
41. Can an object call a class’s constructor more than once?<\/h3>\r\n
42. There are two distinct classes, Class A and Class B. Both classes are in the same package. Can an object of Class B access a private member of Class A?<\/h3>\r\n
43. Can two methods with the same name exist in the same class?<\/h3>\r\n
\r\npublicclassMethodExample{\r\n\r\npublicvoidprint(){\r\n\r\nSystem.out.println(\"Print method without parameters.\");\r\n\r\n}\r\n\r\npublicvoidprint(String name){\r\n\r\nSystem.out.println(\"Print method with parameter.\");\r\n\r\n}\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\nMethodExample obj1 =newMethodExample();\r\n\r\nobj1.print();\r\n\r\nobj1.print(\"xx\");\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n44. How can we duplicate a Java object?<\/h3>\r\n
45. What benefit does employing inheritance provide?<\/h3>\r\n
\r\n\/\/ Superclass\r\n\r\nclassVehicle{\r\n\r\npublicvoidstart(){\r\n\r\nSystem.out.println(\"Vehicle started.\");\r\n\r\n}\r\n\r\n}\r\n\r\n\/\/ Subclass inheriting from Vehicle\r\n\r\nclassCarextendsVehicle{\r\n\r\npublicvoidaccelerate(){\r\n\r\nSystem.out.println(\"Car accelerating.\");\r\n\r\n}\r\n\r\n}\r\n\r\npublicclassMain{\r\n\r\npublicstaticvoidmain(String[] args){\r\n\r\n\/\/ Create an object of the Car class\r\n\r\nCar myCar =newCar();\r\n\r\n\/\/ Access methods from both Vehicle and Car classes\r\n\r\nmyCar.start();\/\/ Inherited from Vehicle class\r\n\r\nmyCar.accelerate();\/\/ Specific to Car class\r\n\r\n}\r\n\r\n}\r\n<\/code><\/pre>\r\n<\/div>\r\n46. What access specifier does a class’s by default for its variables and methods?<\/h3>\r\n
47. Give a Java class example where pointers are used.<\/h3>\r\n
48. How may a class’ inheritance be limited so that no other classes can inherit from it?<\/h3>\r\n