Java programming theory question
Constructor can be inherited, true or false?
False. A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass. A constructor may only be called with new . It cannot be called as a method.
StringBuilder is threadsafe but slower than StringBuffer? True or False
False. StringBuilder is faster than StringBuffer . StringBuffer is mutable. It can change in terms of length and content. StringBuffers are thread-safe, meaning that they have synchronized methods to control access so that only one thread can access a StringBuffer object's synchronized code at a time.
How to define constants in java?
Final
What is garbage collection?
Garbage Collector is a program that manages memory automatically wherein de-allocation of objects is handled by Java rather than the programmer. ... When there are no references to an object, it is assumed to be no longer needed, and the memory, occupied by the object can be reclaimed.
What is the output: System.out.println(1 > 0 : "A":"B");
A
Java Compiler
A Java compiler is a program that takes the text file work of a developer and compiles it into a platform-independent Java file. Java compilers include the Java Programming Language Compiler (javac), the GNU Compiler for Java (GCJ), the Eclipse Compiler for Java (ECJ) and Jikes.
What is the use of packages in Java?
A Package is a collection of related classes. It helps organize your classes into a folder structure and make it easy to locate and use them. More importantly, it helps improve re-usability. Each package in Java has its unique name and organizes its classes and interfaces into a separate namespace, or name group.
What is wrapper class?
A Wrapper class is a class whose object wraps or contains a primitive data types.
What is serialization?
A method of converting a state of objects into a stream.
What is a native method?
A native method is a Java method (either an instance method or a class method) whose implementation is written in another programming language such as C
What is marker interface?
Marker Interface in java is an interface with no fields or methods within it. It is used to convey to the JVM that the class implementing an interface of this category will have some special behavior. Hence, an empty interface in java is called a marker interface.
What is overloading?
Method Overloading is a feature that allows a class to have more than one method having the same name, if their argument lists are different. It is similar to constructor overloading in Java, that allows a class to have more than one constructor having different argument lists.
How to check if a String is representing a number?
System.out.println("\n The type of the variable is : "+xxx.getClass());
What is primitive type and reference type?
The main difference between primitive and reference type is that primitive type always has a value, it can never be null but reference type can be null, which denotes the absence of value.
Why do we need a constructor in Java?
The purpose of constructor is to initialize the object of a class while the purpose of a method is to perform a task by executing java code. Constructors cannot be abstract, final, static and synchronised while methods can be. Constructors do not have return types while methods do.
JDK
Java Development Kit is the core component of Java Environment and provides all the tools, executables and binaries required to compile, debug and execute a Java Program. JDK is a platform-specific software and that's why we have separate installers for Windows, Mac, and Unix systems. We can say that JDK is the superset of JRE since it contains JRE with Java compiler, debugger, and core classes.
JRE
Java Runtime Environment is the implementation of JVM, it provides a platform to execute java programs. JRE consists of JVM and java binaries and other classes to execute any program successfully. JRE doesn't contain any development tools like java compiler, debugger etc. If you want to execute any java program, you should have JRE installed but we don't need JDK for running any java program.
What does the final mean in this method: public void doSomething(final Car aCar){}
The value can't be modified after the variable or statement is classified as final.
Why to override equals and hashCode methods?
hashCode returns based on the object. If the same hashCode is used then that overrides whatever info that previously saved there.
What's the difference beween int and Integer?
int is a primitive variable while Integer is a wrapper class
The protected data can be accessed by subclasses or same package. True or false?
True
What's the difference between "==" and equals method?
= sets the right operator to the left operator. == is a statement that returns a bool if the two statements are identical.
What is IDE and why is it important?
An IDE, or Integrated Development Environment, enables programmers to consolidate the different aspects of writing a computer program. IDEs increase programmer productivity by combining common activities of writing software into a single application: editing source code, building executables, and debugging.
What is String? Is it primitive type?
As we know we have some basic or what we call primitive data types in Java such as int, float , char etc. but we have String as non primitive data type. Also, all Strings default to a value of null because until you give them a value to store at a memory address, there is nothing to be stored. String is a Java Object and not a primitive data type. ... There is no need to define an array of char, just use String.
What is autoboxing?
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
How to call a super class's constructor?
Call to super() must be first statement in Derived(Student) Class constructor. If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass.
What are classes and objects in Java
Class − A class can be defined as a template/blueprint that describes the behavior/state that the object of its type support. Object − Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging the tail, barking, eating. An object is an instance of a class.
What is immutable class?
Immutable class means that once an object is created, we cannot change its content. In Java, all the wrapper classes (like Integer, Boolean, Byte, Short) and String class is immutable.
JVM
Java Virtual Machine is the heart of Java programming language. When we run a program, JVM is responsible for converting Byte code to the machine specific code. JVM is also platform dependent and provides core java functions like memory management, garbage collection, security etc. JVM is customizable and we can use java options to customize it, for example allocating minimum and maximum memory to JVM. JVM is called virtual because it provides an interface that does not depend on the underlying operating system and machine hardware. This independence from hardware and the operating system is what makes java program write-once-run-anywhere.
What is the difference between interface and abstract class?
Main difference is methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behavior. Variables declared in a Java interface is by default final. An abstract class may contain non-final variables.
Can we declare a static variable inside a method?
NO, You can't declare a static variable inside a method, static means that it's a variable/method of a class, it belongs to the whole class but not to one of its certain objects. This means that static keyword can be used only in a 'class scope' i.e. it doesn't have any sense inside methods.
Suppose in previous question, the Car class has a method setColor(Color color){...}, inside doSomething method, Can we call aCar.setColor(red);?
No red needs a datatype Color. Also a method set color needs to be defined in class aCar. If all of this fixed it should run better.
Which class is the super class of all classes?
Object is the super class of all other classes you use, including the ones you implemented.
What is overriding?
Overriding is a feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super-classes or parent classes.
Is parameter passed by value or reference?
Parameters should be passed by reference since it avoids making a new copy of that data when called.
Is Java platform independent
Platform independent language means once compiled you can execute the program on any platform (OS). Java is platform independent. Because the Java compiler converts the source code to bytecode, which is Intermidiate Language. Bytecode can be executed on any platform (OS) using JVM( Java Virtual Machine).
Explain polymorphism.
Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
What is the default value of local variable? What is the default value of instance variable?
There is no default value for local variables, so local variables should be declared and an initial value should be assigned before the first use. Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. Values can be assigned during the declaration or within the constructor.
Which packages are imported by default in Java?
Three packages are imported by default for each source file. First, the package with no name. Second, the java.lang package. And third, the current package (the package in which the current file is defined).
Can an abstract class be defined without any abstract methods?
Yes we can have an abstract class without Abstract Methods as both are independent concepts. Declaring a class abstract means that it can not be instantiated on its own and can only be sub classed. Declaring a method abstract means that Method will be defined in the subclass.
Is Java case sensitive?
Yes, it is case-sensitive. It is this way because of its heritage from C. To keep the language more familiar to what people were used to "in the day", they left it as case-sensitive.
Since there is no way to create an object of abstract class, what's the point of constructors of abstract class?
You can initialize them in the way you want and not use the default elements.