Core Java Questions Flashcards
Explain each keyword in public static void main(String args[])?
a. This is the method that is called by the JVM when starting a program
b. Public - is an access modifier that says that this method is accessible by any class
c. static - says that this method can be accessed without instantiating a class
d. void - the method returns no value
e. String args[] - contains command line arguments that can be passed while running the program
What is an object and what is a class
a. Class - is the template or blueprint through which we can create objects in our programs
b. Object - is an instance of a class that represents an entity that the program is trying to model
What is static in Java
a. Keyword that is used for memory management
b. Can be used with classes, variables, methods and block
c. Static members belong to the class instead of a specific instance
d. Static means one per class
What is a constructor?
a. A special method that is used to instantiate an object
b. It has the same name as the class it is declared in
c. It has no explicit return type
d. It can be overloaded.
Types of constructors
a. Default Constructor - when it has no params and provides default values to the properties of the object
b. Parametized Constructor - used to provide different values to the properties of an object when instantiated
What is this keyword
a. can be used to refer to the current class instance variable
b. can be used to invoke current class method
c. this() can be used to invoke current class constructor
d. can be passed as an argument in the method call
e. can be passed as argument in the constructor
d. can be used to return the current class instance from the method.
What is meant by the local variable and the instance variable
a. Local variable - is a variable that is declared inside a method
b. instance variable - is declared inside a class but outside a method
What is inheritance
a. Process by which a class gains the properties and methods of another class.
What is encapsulation
a. Process of hiding implementation details from other classes.
What is polymorphism
a. The ability to perform an action in several different ways
b. There are two type - Static(compile) time polymorphism and dynamic(runtime) polymorphism
What is method overloading
a. When a class has more than one method with the same name but different signature(different parameters)
What is method overriding
a. When a method of a superclass is inherited by a subclass and the implementation is changed.
What are the different access modifiers
a. Access modifiers - are keywords used to set the visibility of classes, constructors and data members
b. Default - visible only within the package
c. Private - visible within the class only
d. Protected - visible within the package and subclasses
e. Public - visible to all classes within or outside the package
What is an Interface
a. An interface is used to achieve abstraction and loose coupling
b. In Java, a class cannot extend more than one class but with interfaces, a class can implement as many interfaces as possible.
c. interfaces have methods that are abstract by default but have no implementation
d. They can also have variables which can be public, static and final by default.
What is an Abstract Class?
a. A class that is declared using the keyword abstract
b. It can have methods that don’t have a body (abstract) as well as concrete methods(those with a body)
c. An abstract class cannot be instantiated but it must be extended to use the abstract and non abstract methods
What is the difference between Abstract class and Interface?
a. Abstract class can extend only one class or abstract class while Interfaces can extend a number of interfaces
b. An abstract class can have both abstract and concrete methods while an interface can only have abstract methods
c. An abstract keyword is required for an abstract method in an abstract class but in an interface the abstract keyword is optional as the method by default is abstract
d. An abstract class can have protected and public abstract methods while an interface can have only public abstract methods
e. An abstract class can have static, final or static final variables with any access modifies but interfaces can only have public static final as the access modifier
What is the final keyword ?
a. Final variables cannot be changed when initialized
b. Final method cannot be overridden
c. Final class cannot be subclassed
What is a thread
a. The flow of execution of a program
b. They are used to perform tasks concurrently and asynchronously
c. Every java program has at least one thread called the main thread created by the JVM
d. The user can define their own thread by extending the Thread class or implementing the Runnable interface.
What is the difference between String, StringBuffer and StringBuilder?
a. String is a sequence of characters, an immutable object
b. StringBuffer and StringBuilder are classes used to manipulate strings
c. StringBuilder is not thread safe but is faster when compared to StringBuffer
d. StringBuffer is thread safe and synchronized but is slower than StringBuilder.
What is the difference between Array and ArrayList?
a. An array is a fixed length of items while ArrayList is dynamic in length
b. Arrays support primitive data types and objects while Arraylist can only store objects
c. Array performs faster than ArrayList due to being strongly typed
What is the Collection Framework?
a. Is a combination of classes and interfaces that are used to store and manipulate data that is in the form of objects
What is the difference between List and Set in Java
a. List is an interface in java used to create an ordered collection of objects that allows duplicates and null values.
b. List implementations are - ArrayList, LinkedList
c. Set is an interface used to create an unordered collection that doesn’t allow duplicate elements and can only have one null value.
d. Set implantations are - HashSet, TreeSet
What is the difference between Set and Map
a. Set allows for an ordered collection of items while Map contains key value pairs
b. Set contains unique values where Map can contain unique keys with duplicate values.
What is the difference between HashMap and HashTable
a. HashMap is non synchronized while HashTable is synchronized
b. HashTable is thread safe and can be shared between multiple threads
c. HashMap doesn’t guarantee the order of the map will remain constant over time.