In the option b), to make a String valid char literal, we should add prefix "\u" in the string. The anonymous objects do not have any names. 37) Which of the following is a valid syntax to synchronize the HashMap? It contains an abstract method named num() that is not defined properly. The compiler decides which method gets called. } Explanation: In java, the "\w" regex is used to match with a word character consists of [a-zA-Z_0-9]. Explanation: In Java, whenever we divide any number (double, float, and long except integer) by zero, it results in infinity. Two reference variables and two objects are created. The strictfp keyword is a modifier that restricts the floating-point calculations to assure portability and it was added in Java version 1.2. // { Non-virtual or static methods cannot be overridden. Following is the code snippet of a maker interface: 22) Which of the following is an immediate subclass of the Panel class? And the value of variables c and m is 0 as we have not assigned any value to them. The main use of it is to override methods of classes or interfaces. Explanation: A mutable class is a class in which changes can be made after its creation. However, in large applications, in applications maintained by many people and in libraries intended for use by other people, failing to implement these methods correctly can result in classes that cannot be subclassed easily and that do not work as expected. The Java code is portable, as the same byte code can run on any platform. The getClass() method of the Object class returns the runtime class of this object. } For example, \w+ matches one or more word character that is same as ([a-zA-Z_0-9] +). So, the values of i will not be printed, and the above exception is thrown. Hence, the \w regex is used for a word character. Explanation: In the above options, strictfp is the only reserved keyword of Java. class One implements A{ A Java standalone application always starts with a single thread known as the main thread that is associated with the main() method. In Java, main is the method that is the entry point of any program, and the System is a class. A method declared static cannot be overridden but can be re-declared. } Explanation: The program has no error. You do this by redeclaring clone and then calling the superclass's clone method: public Object clone () It is also known as a disassembler. An override method must have the same signature as the overridden base method. The JRE (Java Runtime Environment) is a part of JDK that contains the Java class libraries, Java class loader, and the Java Virtual Machine. When a subclass declares a variable with the same name as of the parent class variable. Instance block will execute whenever an object is created, and they are invoked before the constructors. } Hence, the correct answer is an option (d). Three reference variables and two objects are created. It can be either uppercase (L) or lowercase (l) character. interface A{ The Byte stream is used to perform input or output 8-bit (equals to 1 byte) Unicode bytes whereas, the Character stream is used to read or write a 16-bit (equals to 2 bytes) Unicode character. The extends keyword indicates that the new class is derived from the base or parent class using inheritance, and export is not a keyword in Java. 11 + 5 - 5 + 11 + 3 = 25. JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Explanation: The import keyword is used to access the classes and interfaces of a particular package to the current file. Hence, there are three reference variables and two objects. } Explanation: An interface with no methods and fields is known as the marker interface. In the option c), single quotes are not present. Explanation: The Random class is available in the java.util package. Again, this can result in excess debugging and extra software releases, technical support calls, and possibly lost sales. if (this.data != null) return (p+q); Therefore, in the statement new List (3, true), three (3) refers to the number of rows and true enables the multiple selections. Developed by JavaTpoint. CallableStatement cs = connection.prepareCall("{call.getPassword()}"); CallabledStatement callable = conn.prepareCall("{call getPassword()}"); CallableStatement cab = con.prepareCall("{call getPassword()}"); Callablestatement cstate = connect.prepareCall("{call getpassword()}"); It prints A and B with a 1000 seconds delay between them. But in the output, the values of a and b are 60 and 30, respectively. } Next, I'll consider equals and hashCode together. The second theme is that methods have contracts -- defined behavior -- and when implementing or overriding a method, the contract should be fulfilled. If no external encoding is specified, it will use the default encoding of the host platform. Answer: (c) Map m1 = Collections.synchronizedMap(hashMap); Explanation: By default, the HashMap class is a non-synchronized collection class. 10) What does the expression float a = 35 / 0 return? Answer: (a) for ( int i = 99; i>=0; i / 9), Explanation: The first option is not a valid declaration as i/9 is not declared correctly. 14) Which of the following creates a List of 3 visible items and multiple selections abled? Hence it can't be considered for overloading/overriding. A string representation of an object can be obtained using the toString() method in Java. It is a part of the Java Platform Debugger Architecture (JPDA) that helps in the inspections and debugging of a local or remote Java Virtual Machine (JVM). Global is not a reserved keyword in Java. Which one of the following is not the case of epistasis? Static methods can be overloaded (meaning that you can have the same method name for several methods as long as they have different parameter types). 39) Which of the following is a mutable class in java? In other words, it is a file that contains several components, which make up a self-contained, executable, and deployable jar used to execute Java applications and deploy Java applets. 11) Evaluate the following Java expression, if x=3, y=5, and z=10: Explanation: In the above expression, ++z means that the value will first increment by 1, i.e. Since Cloneable is a tagging interface with no methods, it's simple to implement: public class BaseClass implements Cloneable } cab.executeQuery(); Explanation: In Java, java.sql.CallableStatement interface is used to call the SQL stored procedures in the database. 26) In which memory a String is stored, when we create a string using new operator? An override method provides a new implementation of the method inherited from a base class. It's also not visible on sub-class so cannot be overridden but you can hide the method by declaring another private method of the same signature. Instance block, method, static block, and constructor, Method, constructor, instance block, and static block, Static block, method, instance block, and constructor, Static block, instance block, constructor, and method. public void talk(){ } Yes, We can overload the main method in java but JVM only calls the original main method, it will never call our overloaded main method. The stored procedures are similar to functions as they perform some specific tasks, except that they are only available in the database. The setChar() method is used to replace the original string values with the new one. Explanation: The Javadoc is a tool that is used to generate API documentation in HTML format from the Java source files. public class Student extends Person{ public class Person{ in fact they are not just open by default, they cannot be final in the first place. A final method can be inherited. All Java classes eventually have java.lang.Object, hereafter referred to simply as Object, as a base class. test.display(4, 5.0); All rights reserved. Whereas, the wait() method does not release the locks of an object. The JDK (Java Development Kit) is a software development environment used to develop Java applications and applets. It helps in identifying the cause of the exception. Explanation: There are following reasons for considering a variable shadowing, they are listed below: 18) Which of the following is true about the anonymous inner class? When a method is overridden in the child class. The correct statement will be: The execution order of the program is that the static block executes first, then instance block, and then constructor. We can modify the internal state and fields of a mutable class. 41) What is meant by the classes and objects that dependents on each other? But without assigning the value of i/9 to a variable, it would not execute, and an exception is thrown, as shown below. // Answer: (d) An object that has no reference. Tight coupling is the correct answer as it is used when the logic of one class is called by the logic of another class. The Javadoc command line syntax is, Explanation: There are two types of I/O stream. 47) Which of the following modifiers can be used for a variable so that it can be accessed by any thread or a part of a program? 19) Which package contains the Random class? Explanation: The intern() method is used to return the existing strings from the database. Explanation: A Java ARchive (JAR) is a package file format used to combine all the metadata and resources into a single file. { It also refers to a programming bug or issue that occurs when the thread scheduler swaps the threads at any time between the process. The objects are those variables that are created using the new operator, i.e., studentName and stud_class. A final method cannot be overridden in its subclasses. Explanation: In Java, the return type of hashCode() method is an integer, as it returns a hash code value for the object. } A final variable must be a local variable and cannot be used in other classes. (For a full discussion of shallow copying versus deep copying, see the sidebar below.) Hence, an ArrayList with the default capacity can hold ten (10) values. Explanation: In the above code, we have declared count = 1. 21) An interface with no fields or methods is known as a ______. 20) What do you mean by nameless objects? private SomeOtherClass data; public Object clone () // { final and abstract are not compatible. This article explains why it's important to implement these methods correctly and then explains how to do so. class Test{ Copyright 2022 IDG Communications, Inc. Review: Visual Studio Code shines for Java, Review: Redpanda gives Kafka a run for its money, Comparing Java objects with equals() and hashcode(), Sponsored item title goes here as designed, Java inheritance vs. composition: How to choose, Exploring virtual and abstract methods in C#, How to choose a low-code development platform. Less serious, but still important, implementing these methods incorrectly can result in time lost to debugging. A final class cannot be extended. 16) Which method of the Class.class is used to determine the name of a class represented by the class object as a String? System.out.println("Class One method1"); { Person p = new Student(); Answer: (c) CallableStatement cab = con.prepareCall("{call getPassword()}"); For example, if the string constant pool already has a string object with the same value, the intern() method will return a reference of the string from the pool. public static void main(String args[]){ Answer: (b) Default encoding of the host platform. return length*width; Answer: (c) 8 times *** and 7 times +++++. } One of the major factors of not using pointers in Java is security concerns. If you want newObject to have its own }, Related Questions on Overriding and Overloading, More Related Questions on Overriding and Overloading. public int area(long length, long width){ These are the most difficult to implement correctly. Which among the following can't be used for polymorphism? public class Test{ This is your one-stop encyclopedia that has numerous frequently asked questions answered. The clone method allows clients to obtain a copy of a given object without knowing the precise class of the original object. class Two extends One{ public void talk(){ Half of these methods are final and cannot be overridden. 50) What is the default encoding for an OutputStreamWriter? For polymorphism, function must be property of object, not only of class. The JVM (Java Virtual Machine) enables a computer to run Java or other language (kotlin, groovy, Scala, etc.) The RuntimeException, IOException, and VirtualMachineError classes are the subclasses of the Exception and Error classes. 40) What will be the output of the following program? Explanation: In Java, multiple threads can be executed at the same time. If we assign it to a reference variable like. Y cannot be resolved to a variable. It is, for example, possible to write the clone method so that no child classes can be cloned. Thread-safe means that the methods and objects of a class are accessible by multiple threads at the same time. One is a byte stream, and the other is the character stream. The constructor will execute after the instance block, and it also execute every time the object is created. Our team has collected thousands of questions that people keep asking in forums, blogs and in Google questions. The overridden base method must be virtual, abstract, or override. Which of the following is not a class method? At absolute temperature an intrinsic semiconductor has? One or more additional 'u' characters that are optional. The package keyword is used to create a new package. Explanation: In tight coupling, a group of classes and objects are highly dependent on each other. The value of count will be increased till 14 because of the while (count<=15) statement. Can We Override a Final Method? Finally, if you want some of the member data in the class to be copied deeply, you must copy these members yourself: public class BaseClass implements Cloneable } The rt.jar is the mandatory jar file for every core java application as it contains all the core classes. Because of this, all Java classes inherit methods from Object. Square r = new Square(); Friend function is not a member of the class. Generally, the first exception causes the second exception. 2) Which of the following is not a Java features? No, we cannot override private or static methods in Java. BaseClass newObject = (BaseClass)super.clone(); // At this point, newObject shares the SomeOtherClass 17) In which process, a local variable has the same name as one of the instance variables? This is the reason why Green Team (Java Team members) has not introduced pointers in Java. It is used to tell the JVM or compiler that the particular class has special behavior. A will be printed, and then an exception is thrown. No, the Methods that are declared as final cannot be Overridden or hidden. Tight coupling is also used in some cases, like when an object creates some other objects that are going to be used by them. The overriding just must happen in another interface, and is actually only done to specify more constraints in Javadoc. What is the initial quantity of the ArrayList list? 36) Given that Student is a class, how many reference variables and objects are created by the following code? In other words, the intern() method returns a reference of the string. The non-abstract methods of the superclass are just inherited as they are. The intern() and toString() methods are of String class. public void method1(){ And the object of this class is a thread-safe and cryptographically insecure object. It might look like According to the rules discussed above, an abstract method only has a method signature, not the method body. Hence, the correct answer is the option (b). Answer: (c) notify(), notifyAll(), and wait(). } It acts like a template, or an empty or partially empty structure, you should extend it and build on it before you can use it. Hence, the correct answer is an option (c). Explanation: The three different class methods in Python are static, bounded and unbounded methods. The Random class provides a variety of methods that are used to create random numbers of type integers, float, long, double, etc. Explanation: The sleep() method does not release any locks of an object for a specific time or until an interrupt occurs. // object referred to by this.data with the object Wrapping up the article, I'll describe how to override the comparatively simple toString and finalize methods. public double display(int p, double q){ System.out.print("I am a Student"); The studentId is only a reference variable as it is not declared using the new operator. // Notice that you don't even have to write the clone method! Examples of marker interfaces are Cloneable, Serializable, ThreadSafe, and Remote interface. Explanation: In the above code, we have not defined the variable Y. This situation is known as race condition. So, feel free to use this information and benefit from expert answers to the questions you are interested in! It means when we create an ArrayList without specifying any quantity, it will be created with the default capacity, i.e., 10. } public static void main(String[] args){ It is because the wait() method must be used inside a synchronized block or try-catch block unless it will throw an exception, as shown above. Copyright 2011-2021 www.javatpoint.com. 25) What do you mean by chained exceptions in Java? Answer: (b) An exception caused by other exceptions. 44) How many threads can be executed at a time? Notice how MySubClass has to implement the abstract method abstractMethod() from its abstract superclass MyAbstractClass . programs that are compiled to the Java bytecode. public class Test{ Using the final keyword with a variable makes it constant or immutable. JavaTpoint offers too many high quality services. // } ++z +y -y +z + x++ In Java, the most commonly used marker interfaces are Serializable, Cloneable, Remote, and ThreadSafe interfaces. It is a highly developed set of instructions that are designed to be executed by the Java runtime system known as Java Virtual Machine (JVM). Answer: (a) It returns the existing string from the memory. public class Test extends Two{ 6) _____ is used to find and fix bugs in the Java programs. But on dividing an integer by zero, it throws a runtime exception, i.e., java.lang.ArithmeticException. throws CloneNotSupportedException The clone method in Object is a magic function that generates a shallow copy of the entire object being cloned. Therefore, for all odd numbers till 15 (1, 3, 5, 7, 9, 11, 13, 15), it will print (***), and for all even numbers till 14 (2, 4, 6, 8, 10, 12, 14) it will print (+++++). 3) What should be the execution order, if a class has a method, static block, instance block, and constructor, as shown below? public void method1(); Abstract methods cannot be overridden by a concrete subclass. Now, we have got the complete detailed explanation and answer for everyone, who is interested! System.out.println(r.area(5 , 4)); // copy of data, you must clone this data yourself. 12. An object without having any name but having a reference. Please mail your requirement at [emailprotected] Duration: 1 week to 2 week. } Explanation: The final is a reserved keyword in Java that is used to make a variable, method, and class immutable. Answer: (a) Two bytes read/write at a time. Explanation: From the above statements, the new List(3, true) is the correct answer; this is because of the constructor type. The Runnable, Readable, and Result interface are not marker interface as they contain some methods or fields. The notify() method is used to raise a single thread that is waiting on the object's monitor. }. 1. Explanation: The InterruptedException is thrown when a thread is waiting, sleeping, or occupied. Now, an exception is thrown instead of printing B. The notifyAll() method is similar to the notify() method, except that it wakes up all the threads that are waiting on the object's monitor. The char literals are always declared in single quotes ('). Whereas when we create a string using double quotes, it will check for the same value as of the string in the string constant pool. class Square extends Rectangle{ throws CloneNotSupportedException We may further divide the classes into parent class and subclass if inheritance is used. Hence, an asterisk (***) will be printed eight times, and plus (+++++) will be printed seven times. Explanation: In Java, we can modify the values of a variable with the help of a reserved keyword known as volatile. Hence, the "o" and "e" are replaced by the characters "i" and "d," respectively, which results in "Cimpletd.". Explanation: Following are some rules for declaring an abstract method: In the above code, MyFirstClass is an abstract class. It is a different way of making a class thread-safe. Explanation: In Java, Unicode characters can be used in string literals, comments, and commands, and are expressed by Unicode Escape Sequences. 9) Which of the following is a valid long literal? However, it is recommended to use uppercase character instead of lowercase because the lowercase (l) character is hard to distinguish from the uppercase (i) character. // return super.clone(); In the option d), "\i" is used in place of "\u.". // SomeOtherClass is just an example. A subclass within the same package as the instance's superclass can override any superclass method that is not declared private or final. The important features of the final keyword are: 24) Which of these classes are the direct subclasses of the Throwable class? Explanation: The nameless objects are basically referred to as anonymous objects. To synchronize the HashMap class explicitly, we should use the Collections.synchronizedMap(hashMap) method that returns a thread-safe map object. Answer: (a) Bytecode is executed by the JVM. Map m1 = Collections.synchronizedMap(hashMap); Map m2 = Collection.synchronizeMap(hashMap); Abstract methods do not specify a method body, but they only have a method signature. As per the above paragraph, you can not override the private method in Java because it's bonded during compile time using static binding. One reference variable and two objects are created. Explanation: In the above code, there are three reference variables and two objects. If not, you should either rewrite your implementations to be correct in child classes or declare your class to be final so that there are no child classes. }, what is the result of the following piece of code: clone is a protected method. The first import statement imports all the classes and interfaces of java.awt package. If a method cannot be inherited, then it cannot be overridden. So, what mistakes should you look out for? Explanation: According to the class hierarchy of Throwable class, the Error and Exception classes are the direct subclasses of the Throwable class, as shown below. Incorrect implementation of these methods can also result in intermittent, data-dependent bugs as behavior changes over time. An object of the Random class is used to generate a series of pseudorandom numbers. }, What will be the output of the following program code? { Explanation: A marker interface is an interface with no fields and methods. Similarly, there are two values of variable b, i.e., 5 and 30. Answer: (b) It does not release any locks. For example, new Employee();. The regex \W, \s, and \S are used for a non-word character, a whitespace character, and a non-whitespace character, respectively. The character encoding can be either a default encoding dependent on the system or encoding that is explicitly defined. If the class is provided as part of a class library you sell to other programmers, you may find yourself rereleasing your library, handling excess technical support calls, and possibly losing sales as customers discover that your classes can't be extended. You can go through the link, (https://www.javatpoint.com/java-swing) to deeply understand the class hierarchy diagram. To enable shallow cloning of your class, you implement the Cloneable interface. Explanation: In the above code, there are two values of variable a, i.e., 10 and 60. It is because of the execution order of the program. 4) What will be the output of the following program? In the operating system, only one thread is executed at a time. { Abstract methods are always defined inside an abstract class. After that, the thread will wait for 1000 seconds. The Panel class and Window class are the child classes of the Container class, and Frame and Dialog classes are the subclasses of the Window class. These methods all are final and cannot be overridden. // } In other words, an empty interface (contains nothing) is known as the marker interface. Test test = new Test(); Explanation: A char literal may contain a Unicode character (UTF-16). The purpose of an abstract class is to function as a base for subclasses. In other words, it is used to import the user-defined and built-in classes and interfaces into the source file of java so that the current file can easily access the other packages by directly using its name. It generally consists of classes like java.lang.String, java.lang.Object, java.io.Exception, etc., and all packages and classes available in the rt.jar are known to the JVM. Can deionized water be used instead of distilled. 30) Which keyword is used for accessing the features of a package? Now, evaluate the statement by putting the values of x, y, and z. Implementing toString improperly is the least damaging, but can still result in loss of time, as you must debug if the name of the object is wrong. When overriding equals, you are responsible for ensuring that all the specifics of the contract are still met. Explanation: If two or more threads are trying to access a common resource at the same time. public void display(int x, double y){ The static block will execute whenever the class is loaded by JVM. Explanation: The OutputStreamWriter class translates Unicode character into bytes by using the character encoding. For this very reason, a method must be declared as final only when we're sure that it is complete. Mail us on [emailprotected]nt.com, to get more information about given services. A final class cannot extend other classes. It does not have any fixed came name. Answer: (b) Method is not defined properly. IOException and VirtualMachineError class, Exceptions occurred by the VirtualMachineError, Exceptions occur in chains with discarding the debugging information, It returns the existing string from memory, It modifies the existing string in the database, All the classes available in rt.jar is known to the JVM, notify(), wait( long msecs ), and synchronized(), wait( long msecs ), interrupt(), and notifyAll(), sleep( long msecs ), wait(), and notify(). // class SomeOtherClass implements Cloneable The need for synchronization is to perform thread-safe operations on the class. The String, Byte, and Short are immutable classes as they cannot be altered once they are created. If you want objects from other packages to be able to call it, you must make clone public. return super.clone(); In C++, virtual functions can be private and can be overridden by the derived class. But a final class can extend other classes. When we define a variable in a local scope with a variable name same as the name of a variable defined in an instance scope. To create a list of 3 visible items along with the multiple selections abled, we have to use the following constructor of the List class. What is output of the program? 45) If three threads trying to share a single object at the same time, which condition will arise in this scenario? Explanation: According to the class hierarchy of Java Swing, the Applet class is the direct subclass of the Panel class. Whereas, the second import statement only imports the Object class of the java.lang package. }, What will be the output? The volatile keyword is not a replacement of a synchronized block or method as it does not remove the need for synchronization among the atomic actions. No, a private method cannot be overridden because the subclass doesn't inherit its parent's private members. Which of the following is not type of class? Hence, the correct answer is the option (a). }. Private, static and final cant be overridden. 1) In Java, inner Class is allowed to access private data members of outer class. 29) Which of the following is a reserved keyword in Java? Explanation: The initial or default quantity of an ArrayList is 10. System.out.print("I am a Person"); Therefore, a single operation of character stream performs two bytes read/ write at a time. Two themes will reappear throughout this article. } Explanation: The output of the Java compiler is bytecode, which leads to the security and portability of the Java code. Three reference variables and three objects are created. 15) Which of the following for loop declaration is not valid? // Rest of the class.
- Tyrone Mings Family Tree
- Agile Combat Employment Doctrine
- Loan Receivable Debit Or Credit
- Yesterday's Maximum Temperature
- Nerf Super Soaker Robo Blaster Instructions
- Jackery Explorer 1500
- Rutgers Housing Payment
- Natural Springs Florida
- Blazing Saddles Cycling Club
- Harper College Alumni