Disclaimer: “Sources Unknown”; Compilation only; I compiled it from various sources When I was exploring Java.
Question: What is the importance of static variable?
Answer: static variables are class level variables where all objects of the class refer to the same variable. If one object changes the value then the change gets reflected in all the objects.
Question: Can we declare a static variable inside a method?
Answer: Static variables are class level variables and they can’t be declared inside a method. If declared, the class will not compile.
Question: What is an Abstract Class and what is its purpose?
Answer: A Class which doesn’t provide complete implementation is defined as an abstract class. Abstract classes enforce abstraction.
Question: Can a abstract class be declared final?
Answer: Not possible. An abstract class without being inherited is of no use and hence will result in compile time error.
Question: What is use of a abstract variable?
Answer: Variables can’t be declared as abstract. only classes and methods can be declared as abstract.
Question: Can you create an object of an abstract class?
Answer: Not possible. Abstract classes can’t be instantiated.
Question: Can a abstract class be defined without any abstract methods?
Answer: Yes it’s possible. This is basically to avoid instance creation of the class.
Class C implements Interface I containing method m1 and m2 declarations. Class C has provided implementation for method m2. Can i create an object of Class C?
No not possible. Class C should provide implementation for all the methods in the Interface I. Since Class C didn’t provide implementation for m1 method, it has to be declared as abstract. Abstract classes can’t be instantiated.
Question: Can a method inside a Interface be declared as final?
Answer: No not possible. Doing so will result in compilation error. public and abstract are the only applicable modifiers for method declaration in an interface.
Question: Can an Interface implement another Interface?
Answer: Interfaces doesn’t provide implementation hence a interface cannot implement another interface.
Question: Can an Interface extend another Interface?
Answer: Yes an Interface can inherit another Interface, for that matter an Interface can extend more than one Interface.
Question: Can a Class extend more than one Class?
Answer: Not possible. A Class can extend only one class but can implement any number of Interfaces.
Question: Why is an Interface be able to extend more than one Interface but a Class can’t extend more than one Class?
Answer: Basically Java doesn’t allow multiple inheritance, so a Class is restricted to extend only one Class. But an Interface is a pure abstraction model and doesn’t have inheritance hierarchy like classes(do remember that the base class of all classes is Object). So an Interface is allowed to extend more than one Interface.
Question: Can an Interface be final?
Answer: Not possible. Doing so will result in compilation error.
Question: Can a class be defined inside an Interface?
Answer: Yes it’s possible.
Question: Can an Interface be defined inside a class?
Answer: Yes it’s possible.
Question: What is a Marker Interface?
Answer: An Interface which doesn’t have any declaration inside but still enforces a mechanism.
Question: Which OO Concept is achieved by using overloading and overriding?
Question: If i only change the return type, does the method become overloaded?
Answer: No it doesn’t. There should be a change in method arguments for a method to be overloaded.
Question: Why does Java not support operator overloading?
Answer: Operator overloading makes the code very difficult to read and maintain. To maintain code simplicity, Java doesn’t support operator overloading.
Question: Can we define private and protected modifiers for variables in interfaces?
Question: What is a local, member and a class variable?
Answer: Variables declared within a method are “local” variables. Variables declared within the class i.e. not within any methods are “member” variables (global variables). Variables declared within the class i.e. not within any methods and are defined as “static” are class variables
Question: What is the range of the short type?
Answer: The range of the short type is -(2^15) to 2^15 – 1.
Question: How is rounding performed under integer division?
Answer: The fractional part of the result is truncated. This is known as rounding toward zero.
Question: What are the legal operands of the instanceof operator?
Answer: The left operand is an object reference or null value and the right operand is a class, interface, or array type.
Question: Are true and false keywords?
Answer: The values true and false are not keywords.
Question: What is the difference between an if statement and a switch statement?
Answer: The if statement is used to select among two alternatives. It uses a boolean expression to decide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternative should be executed.
Question: What are the practical benefits, if any, of importing a specific class rather than an entire package (e.g. import java.net.* versus import java.net.Socket)?
Answer: It makes no difference in the generated class files since only the classes that are actually used are referenced by the generated class file. There is another practical benefit to importing single classes, and this arises when two (or more) packages have classes with the same name. Take java.util.Timer and javax.swing.Timer, for example. If I import java.util.* and javax.swing.* and then try to use “Timer”, I get an error while compiling (the class name is ambiguous between both packages). Let’s say what you really wanted was the javax.swing.Timer class, and the only classes you plan on using in java.util are Collection and HashMap. In this case, some people will prefer to import java.util.Collection and import java.util.HashMap instead of importing java.util.*. This will now allow them to use Timer, Collection, HashMap, and other javax.swing classes without using fully qualified class names in.
Question: Can a method be overloaded based on different return type but same argument type ?
Answer: No, because the methods can be called without using their return type in which case there is ambiguity for the compiler
Question: What happens to a static var that is defined within a method of a class ?
Answer: Can’t do it. You’ll get a compilation error
Question: What is constructor chaining and how is it achieved in Java ?
Answer: A child object constructor always first needs to construct its parent (which in turn calls its parent constructor.). In Java it is done via an implicit call to the no-args constructor as the first statement.
Question: Explain the usage of Java packages.
Answer: This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes.
Question: Explain in your own words the “bottom line” benefits of the use of an interface.
Answer: The interface makes it possible for a method in one class to invoke methods on objects of other classes, without the requirement to know the true class of those objects, provided that those objects are all instantiated from classes that implement one or more specified interfaces. In other words, objects of classes that implement specified interfaces can be passed into methods of other objects as the generic type Object, and the methods of the other objects can invoke methods on the incoming objects by first casting them as the interface type.
Question: What are some advantages and disadvantages of Java Sockets?
Answer: Some advantages of Java Sockets:
Sockets are flexible and sufficient. Efficient socket based programming can be easily implemented for general communications. Sockets cause low network traffic. Unlike HTML forms and CGI scripts that generate and transfer whole web pages for each new request, Java applets can send only necessary updated information.
Question: Some disadvantages of Java Sockets:
Answer: Security restrictions are sometimes overbearing because a Java applet running in a Web browser is only able to establish connections to the machine where it came from, and to nowhere else on the network Despite all of the useful and helpful Java features, Socket based communications allows only to send packets of raw data between applications. Both the client-side and server-side have to provide mechanisms to make the data useful in any way.
Question: What is OOPS?
Answer: OOP is the common abbreviation for Object-Oriented Programming.
There are three main principals of oops which are called Polymorphism, Inheritance and Encapsulation.
Question: What is a working thread? –
Answer: A working thread, more commonly known as a worker thread is the key part of a design pattern that allocates one thread to execute one task. When the task is complete, the thread may return to a thread pool for later use. In this scheme a thread may execute arbitrary tasks, which are passed in the form of a Runnable method argument, typically execute(Runnable). The runnable tasks are usually stored in a queue until a thread host is available to run them. The worker thread design pattern is usually used to handle many concurrent tasks where it is not important which finishes first and no single task needs to be coordinated with another. The task queue controls how many threads run concurrently to improve the overall performance of the system. However, a worker thread framework requires relatively complex programming to set up, so should not be used where simpler threading techniques can achieve similar results.
Question: What is a green thread?
Answer: A green thread refers to a mode of operation for the Java Virtual Machine (JVM) in which all code is executed in a single operating system thread. If the Java program has any concurrent threads, the JVM manages multi-threading internally rather than using other operating system threads. There is a significant processing overhead for the JVM to keep track of thread states and swap between them, so green thread mode has been deprecated and removed from more recent Java implementations. Current JVM implementations make more efficient use of native operating system threads.
Question: What is the volatile modifier for?
ANSWER: The volatile modifier is used to identify variables whose values should not be optimized by the Java Virtual Machine, by caching the value for example. The volatile modifier is typically used for variables that may be accessed or modified by numerous independent threads and signifies that the value may change without synchronization.
Question: What is Internationalization?
Answer: It is the process of designing an application so that it can be adapted to various languages and regions without engineering changes. Sometimes the term internationalization is abbreviated as i18n, because there are 18 letters between the first “i” and the last “n.”
Localization is the process of adapting software for a specific region or language by adding locale-specific components and translating text. The term localization is often abbreviated as l10n, because there are 10 letters between the “l” and the “n.”
Java can display any Unicode characters through its windowing system AWT, provided that 1. You set the Java system property “user.language” appropriately, 2. The /usr/lib/java/lib/font.properties.language font set definitions are appropriate, and 3. The fonts specified in that file are installed. For example, in order to display text containing japanese characters, you would install japanese fonts and run “java -Duser.language=ja …”. You can combine font sets: In order to display western european, Greek and japanese characters simultaneously, you would create a combination of the files “font.properties” (covers ISO-8859-1), “font.properties.el” (covers ISO-8859-7) and “font.properties.ja” into a single file. ??This is untested??
The interfaces java.io.DataInput and java.io.DataOutput have methods called `readUTF’ and `writeUTF’ respectively. But note that they don’t use UTF-8; they use a modified UTF-8 encoding: the NUL character is encoded as the two-byte sequence 0xC0 0×80 instead of 0×00, and a 0×00 byte is added at the end. Encoded this way, strings can contain NUL characters and nevertheless need not be prefixed with a length field – the Cfunctions like strlen() and strcpy() can be used to manipulate them.
Other interesting FAQs related Posts on Java, you may like:
Java Notes – 1 (clean-clouds.com)
Java Notes – 2 (clean-clouds.com)
Java Notes – 3 (clean-clouds.com)
Java Notes -4 (clean-clouds.com)
Java Notes -5 (clean-clouds.com)
Java Notes -6 (clean-clouds.com)
Frequently Asked Questions in Java Part- 1 (clean-clouds.com)
Frequently Asked Questions in Java Part- 2 (clean-clouds.com)
Frequently Asked Questions in Java Part-3 (clean-clouds.com)
Frequently Asked Questions in Java Part-4 (clean-clouds.com)
Frequently Asked Questions in Java Part-5 (clean-clouds.com)
Frequently Asked Questions in Java Part-6 (clean-clouds.com)
Frequently Asked Questions in Java Part-7 (clean-clouds.com)
Frequently Asked Questions in Java Part-8 (clean-clouds.com)
Frequently Asked Questions in Java Part-9 (clean-clouds.com)
Frequently Asked Questions in Java Part-10 (clean-clouds.com)
Frequently Asked Questions in Java Part-11 (clean-clouds.com)
Frequently Asked Questions in Java Part-12 (clean-clouds.com)
Frequently Asked Questions in Java Part-13 (clean-clouds.com)
Frequently Asked Questions in Java Part-14 (clean-clouds.com)