Get Latest Exam Updates, Free Study materials and Tips
1.What is Java?
Ans: Java is a programming language It is a high-level programming language. It can be characterized by the following words-Simple. Object-oriented, Architecture neutral, Portable, Distributed, High performance, Multithreaded, Secure, Dynamic, Robust
2.What is platform-independent?
Ans: Platform independence means, the Java source code can run on all operating systems. “write once and run anywhere is called platform independent.
3.How many types of memory are allocated by M7
Ans: Stack, Heap, Class Area, Native Method stack, Program Counter Register.
4.What is Constructor?
Ans: A constructor is a method whose name is the same as the class name whose task is to initialize an object of its class. It does not have a return type and they cannot return values. They cannot be inherited, though a derived class you can call the base class constructor The constructor is invoked when you create an object of its class
5. What is class Variables?
Ans: Variables are declared within a class, outside of any method, with static keywords are called class variables.
6. Can constructors be inherited?
Ans: No, a constructor cannot be inherited.
7. How does the Java default constructor be provided?
Ans: If a class does not have any constructor, the compiler will automatically provide the default constructor.
8. What is an Object?
Ans: The object is a runtime entity and it has stated ( as a field) and behaviour (method).
9.What is an Instance Variable?
Ans: Instance variables are declared within a class but outside any method. these variables are instantiated when the class is loaded.
10. What is a local variable?
Ans: Variables are defined inside blocks, constructors or methods, called local variables. local variables are declared and initialized within the method and they will be destroyed when the method has been completed.
11. What is a static variable?
Ans: Declared with a static keyword that is outside a method and inside a class is called a static variable.
12.What do you mean by access modifier?
Ans: Java provides an access modifier to access classes, variables, methods and constructors.
13. Why string is immutable?
Ans: The String class is immutable so that once it is created a String object cannot be changed. A string is immutable it can safely be shared between many threads, which is very important for multithreaded programming.
14. What is the difference between StringBuffer and StringBuilder class?
Ans: Use StringBuilder whenever possible because it is faster than StringBuffer. But, if thread safety is necessary then use StringBuffer objects.
15. What is finalize?
Ans: finalize() is a method that is called before the Garbage collector reclaims the Object, its the last chance for any object to perform cleanup activity i.e. releasing any system resources held, closing the connection if open.
16.What is a runtime exception?
Ans: It is an exception that occurs that probably could have been avoided by the programmer. As opposed to checked exceptions, runtime exceptions are ignored at the time of compilation.
17. What is Inheritance?
Ans: Inheritance is one of the features of Object-Oriented Programming. Inheritance allows a class to use the properties and methods of another class or interface.
18.When a super keyword is used?
Ans: If the method overrides its superclass’s methods, the overridden method can be invoked through the use of the keyword super.
19. What is Polymorphism?
Ans: In java, we can perform a single action in different ways. there are two types of polymorphism. runtime polymorphism and compile-time polymorphism. runtime polymorphism can be achieved by method overriding and compile-time polymorphism is achieved by method overloading.
20. What is Abstraction?
Ans: Abstraction is the process of abstraction is used to hide complex details and only show the essential features of the object.
21. What is an abstract class?
Ans: Abstract class contains one or more abstract methods which are method declarations without a body.
22.What are Access specifiers in java?
Ans: Public – public classes, methods, and fields can be accessed from everywhere.
Private-private methods and fields can only be accessed within the same class to which the methods and fields belong. private methods and fields are not visible within subclasses and are not inherited by subclasses.
Protected-protected methods and fields can only be accessed within the same class to which the methods and fields belong, within its subclasses, and within classes of the same package.
Default – If you do not set access to a specific level, then such a class, method, or field will be accessible from inside the same package to which the class, method, or field belongs, but not from outside this package.
23. What is the final modifier?
Ans: final Classes-final class cannot have subclasses. final Variables-final variable cannot be changed once it is initialized, final Methods-final method cannot be overridden by subclasses.
24. What are static methods?
Ans: Methods declared with the keyword static as modifiers are called static methods. A static method can only call other static methods.
25. What are static variables?
Ans: Static Variables that have only one copy per class. They are declared by using the static keyword as a modifier.
26. What is the final modifier?
Ans: final Classes-final class cannot have subclasses. final Variables-final variable cannot be changed once it is initialized. final Methods-final method cannot be overridden by subclasses.
27. What are static methods?
Ans: Methods declared with the keyword static as modifier are called static methods. A static method can only call other static methods.
28. What are static variables?
Ans: Static Variables that have only one copy per class. They are declared by using the static keyword as a modifier.
29. Can I have multiple main() methods in the same class?
Ans: No, the program fails to compile.
30. Do I need to import java.lang package any time? Why?
Ans: No. It is loaded internally by the JVM.
31. What is Overriding?
Ans: When a derived class defines a method using the same name, return type, and parameters as a method in its superclass, the method in the class override the method in the superclass.is called Overriding.
32. What are wrapper classes?
Ans: Java provides special classes corresponding to each of the primitive data types. These are called wrapper classes. example: Integer, Character, Double etc.
33.Why do we need wrapper classes?
Ans: It is easy to deal with primitives as objects., some of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these reasons, we need wrapper classes. so that we create instances of these classes we can store it in any of the collection classes and pass it as a collection. Also, we can pass it as method parameters where a method expects an object.
1.What is OOPS?
Ans: Object-Oriented Programming is a concept to build a program. Object-oriented Programming is a programming method that combines data and instructions which are used within a program. These are the concepts of OOPS:
(1)Abstraction
(2) Encapsulation
(3) Polymorphism
(4)Inheritance
(5)Association
(6)Composition
(7)Aggregation
2. What is Abstraction?
Ans: Abstraction is the concept of hiding the internal details and showing functionality in simple manner. A Java program is also an example of abstraction.
3. What is Encapsulation?
Ans: Encapsulation is a mechanism of wrapping data as a single unit. In Encapsulation variables of a class are hidden from other classes, and can be accessed from their methods of the same class. therefore, it is also called Data Hiding. We can achieve encapsulation using setter and getter methods.
4.What is Polymorphism?
Ans: Polymorphism is a concept in java. lt is a concept to achieve the same action in many ways. Two types of polymorphism in java. (a)Runtime Polymorphism(Method overriding). (b)Compile-time Polymorphism(Method overloading).
5. What is Runtime polymorphism?
Ans: When a derived class extends the superclass, the derived class contains the data and methods that are already defined in the superclass In other words, the child class object has all the attributes of its parent class, which is called Runtime polymorphism.
6. What is Compile time polymorphism?
Ans: Method overloading is an example of compile-time polymorphism. Method overloading is also known as Static Polymorphism.it is also known as static binding. It happens at compile time.
7. What is Inheritance?
Ans: Inheritance is an object-oriented programming concept where one class inherits the property of another class or interface.
8.What is Association?
Ans: It defines the relationship between objects. which can be of any type say one to one, one too may etc. An aggregation is a special form of association which is a one-way relationship between classes.
9.What is Composition?
Ans: In Composition two entities (classes) are highly dependent on each other. For e.g. Bike and Engine. Without the engine, the bike cannot work.
10.What is an Object?
Ans: Object means a real word entity such as a book, laptop, mobile etc. Objects have states and behaviours Example: A person has states-colour, name and behaviour. An object is an instance of a class.
11. What is Class?
Ans: A class is a blueprint from which objects are created.
12. What is method overloading?
Ans: When we create more than one method by the same name with different no. of arguments in the same class is called method overloading. Example:- public void hello()0 public void hello(int i,int j )) public void hello(int i,int j,int k).
13. What is method overriding?
Ans: When a derived class has same the method with the same no. of arguments as its superclass’s method is called method overriding. It is achieved by inheritance.
14.What is an abstract class?
Ans: An abstract class cannot be instantiated. It can be inherited but the creation of an object is not possible with abstract class. it may or may not include abstract methods. An abstract class is used to provide abstraction.
15. What is the abstract method?
Ans: The method that is declared without anybody within an abstract class is called the abstract method. Example:- abstract void display();
16. What is an interface?
Ans: An interface is a collection of abstract methods. It is similar to a class. An interface is not extended by a class, it is implemented by a class. An interface can extend multiple interfaces. The interface cannot be instantiated. An interface does not contain any constructors.
17.What is the difference between object-oriented programming language and object-based programming language?
Ans: Object-based programming languages follow all the features of OOPS except for Polymorphism or inheritance. Examples of object-based programming languages are JavaScript, VBScript etc.
18. What is the final class?
Ans: The final class can’t be inherited.
19.Is the final method inherited?
Ans: Yes, the final method is inherited but you cannot override it.
20. Can we declare a constructor final?
Ans: No, because the constructor is never inherited.
21.What is the final variable?
Ans: If you make a variable as a final you cannot change the value of a final variable, it will be constant.
22.What is the difference between static binding and dynamic binding?
Ans: The static binding type of object is determined at compile time. But in the dynamic binding type of object is determined at runtime.
23. Can an Interface be final?
Ans: No. because it is implemented by another class. If you will declare the interface as a final, you cannot use it through implements by another class.
24.Can we define private and protected modifiers for variables in interfaces?
Ans: No, by default they are public.
25. What are access modifiers?
Ans: Access modifiers demonstrate the scope of the methods or variables that can be accessed from other classes. There are 4 types of access modifiers, they are as follows:
(1)Private.
(2)Protected.
(3)Public.
(4)Default.
There are many non-access modifiers such as static, abstract, native, volatile, synchronized, transient etc.
26.Why use the clone () method?
Ans: The clone() method saves the extra processing task for creating the same copy of an object. The advantage of object cloning is fewer processing tasks.
1. What is String in Java? The string is a data type?
Ans: The string is a Class in java and defined in java.lang package. It’s not a primitive data type like int and long, char, float.
2. Is String a keyword in java?
Ans: No. The string is not a keyword in java. It is a Class.
3. Is String a primitive type or derived type?
Ans: The string is a derived type.
4.In how many ways you can create string objects in java?
Ans: There are two types to create string objects in java. One is using a new keyword and another one is using string literals. The objects created using new keywords are stored in the heap memory and objects created using string literals are stored in string String s1 = new String(” preparing for String s2 = ‘doing well”; constant pool. Example: Java”);
5. How can we make String upper case or lower case?
Ans: We can use String class’s toUpperCase and toLowerCase methods to get the String in all upper case or lower case.
6. How to compare two Strings in a java program?
Ans: Java String implements a Comparable interface and it has two variants of compareTo() methods. compareTo(String str2) method compares the String object with the String argument passed lexicographically. If the String object precedes the argument passed, it returns a negative integer and if the String object ws the argument The string passed, it returns a positive integer. It returns zero when both the String have the same value, in this case, the equals(String st) method will also return true.
7. How to convert String to byte array and vice versa?
Ans: We can use the String getBytes() method to convert String to byte array and we can use String constructor new String(byte[] arr) to convert byte array to String.
8.Difference between String, String Buffer and StringBuilder?
Ans: The string is immutable and final in Java, so whenever we use String manipulation, it creates a new String Java provides two utility classes for String manipulations: -StringBuffer and StringBuilder. StringBuffer and StringBuilder are mutable classes. StringBuffer operations are thread-safe and synchronized where StringBuilder operations are not thread-safe.
9.Why String is immutable or final in Java?
Ans: It increases security for storing sensitive information such as database username, password etc.
10. How to Split String in java?
Ans: In the String class, there is a split method, the split(String rgx) method can use to split the String into String array based on the given regular expression.
11.How do you check if two Strings are equal in Java?
Ans: Using equals() method. When we use == operator, it checks for the value of the String as well as reference but in our programming, most of the time we check the equality of the String for value only. So we use the equals method to check if two Strings are equal or not.
Example:
String s1= “Java”; String s2= “Java”;
String s3= new String(“Java”);
System.out.println(‘s1 == $2? + ($1==$2)); //true System.out.println(‘s1 == s3? *+
($1==$3)); //false
System.out.println(‘s1 equals s3?*+ (s1.equals(s3))); //true
12. What is String Pool?
Ans: A string Pool is a pool of Strings stored in Java heap memory.
13.Does String is thread-safe in Java?
Ans: Strings are immutable, so we can’t change their value. Hence it’s thread-safe and can be used in a multi-threaded environment.
14.What do you mean by mutable and immutable objects?
Ans: Immutable objects are like constants. You can’t modify them once they are created. They are final. Whereas mutable objects can be modified.
15. Which is the final class in these three classes-String, StringBuffer and StringBuilder?
Ans: All are final classes. but String is immutable and StringBuffer and StringBuilder are mutable.
16. How do you create mutable string objects?
Ans: Using StringBuffer and StringBuilder classes. These classes provide mutable string objects.
17. Which class will you recommend among String, StringBuffer and StringBuilder classes if I want mutable and thread-safe objects?
Ans: StringBuffer
18. How many objects will be created in the following code and where they will be stored? String s1= new String(“hello java”); String s2 = “hello java”;
Here, two String objects will be created. The object created using the new keyword(s1) will be stored in the heap memory. The object created using the string literal(s2) will be stored in the string constant pool.
19. What is the difference between Java strings and C, C++ strings?
Ans: In C and C++, strings are terminated with a null character. But in Java, strings are not terminated with a null character. Strings are treated as objects in Java.
1. What is multithreading?
Ans: Multithreading is a process of executing multiple threads simultaneously. The thread is lightweight. The thread is a lightweight sub-process, the smallest unit of processing. Multithreading is used to achieve multitasking. Threads share a common memory area. They don’t allocate separate memory areas so it saves memory.
2. What is the advantage of java multithreading?
Ans: It doesn’t stop the user because threads are independent and you can perform multiple tasks at the same time. You can perform many operations at the same time so it saves time. Threads are independent so it doesn’t affect other threads.
3.What is Multitasking?
Ans: Multitasking is a process to execute multiple tasks simultaneously. Multitasking can be achieved in two ways: multitasking(Multiprocessing) – Each
(1) Process-based process takes a separate memory area. It is heavyweight. the cost of communication between processes is high.
(2) Thread-based multitasking (Multithreading) – Threads uses the same address space. It’s lightweight and cost are low.
4. What is Thread in java?
Ans: A thread is a lightweight sub-process, it is a separate way. They are independent, if any exception occurs in one thread another thread will not affect, means that will run properly.
5.Life Cycle of a Thread or Explain the state of a thread.
Ans: There are 4 states in the thread lifecycle in Java – new, runnable, non-runnable and terminated. There is no running state.
1. New – Thread is in a new state when you create an instance of Thread class and before the invocation of the start() method.
2. Runnable – After invocation of the start() method, the thread is in the runnable state.
3. Non-Runnable – In this state thread will be alive, but it is currently not available to run.
4. Terminated -After the run() method exits, the thread is in the dead state or you can say it is in the terminated state.
6. How many ways to create a thread?
Ans: Two ways to create a thread: 1. You extend Thread class 2. You implement a Runnable interface.
7. What is Thread Class and write some names of methods of Thread class?
Ans: The thread class provide constructors and methods to create and perform operations on a thread. Methods of Thread class:
public void run()
public void start()
public void sleep(long mils)
public void join()
public void join(long milliseconds) public void get priority
public void setPriority (int priority) public void stop()
public void resume() public void suspend()
public void interrupt public void setDaemon(boolean b)
public void is interrupted(), etc…
8.What is Thread Scheduler in Java?
Ans: The thread scheduler decides which thread should run.
9. Difference between preemptive scheduling and time slicing?
Ans: In preemptive scheduling, the highest priority task will execute until it enters the waiting state. In time slicing, a task executes for a predefined slice of time.
10. What is the sleep() method in Java?
Ans: The sleep() method of Thread class is used to sleep a thread for a certain amount of time.
11. Can we start a thread twice?
Ans: No. it will throw an exception (IllegalThreadStateException).
12.When do we use Runnable and Thread in Java?
Ans: Java programming language doesn’t support multiple inheritances of class, but it allows you to implement multiple interfaces. This means, it’s good to implement Runnable then extends Thread if you have to extend another class.
13.What is the difference between Runnable and Callable in Java?
Ans: Runnable and Callable both represent task which is executed in a separate thread. Runnable is present from JDK 1.0 while Callable was added on JDK 1.5. The difference between these two is that Callable’s call() method can return value and throw Exception, which is not possible with Runnable’s run() method.
14. What is thread-safety? is Vector a thread-safe class?
Ans: Thread safety is a property of an object which guarantees that if executed it will behave as expected. Vector is a thread-safe class and it is achieved by synchronizing methods that modify the state of Vector, ArrayList is not thread-safe.
15.What is race condition?
Ans: Race conditions are programming bugs when Java programs are in the concurrent execution environment. a race condition occurs due to a race between multiple threads, suppose if a thread which is to execute first lost the race and executed second.
16.What happens when an exception occurs in a thread?
Ans: If you are not caught thread will die, if an uncaught exception handler is presented then it will get a callback.
17.How do you share data between two threads in Java?
Ans: By using the shared objects you can share data, or using concurrent data structures like BlockingQueue.
18.What is the difference between notify and notifyAll in Java?
Ans: There notify() method notify only one thread. but, notifyAll() sends a notification to all threads and allows them to compete for locks, which ensures that at least one thread will proceed further.
19. How do you check if a thread holds a lock or not?
Ans: There is a method called holdsLock( on java.lang.Thread, it returns true if the current thread holds the monitor lock on the specified object.
20.What is Threadinterruption (or Interrupting thread) in java?
Ans: If any thread is in sleeping or waiting for the state (i.e. sleep() or wait() ), then calling the interrupt() method on the thread, breaks out the sleeping or waiting for state and it will throw Interrupted Exception.
21.What is a Volatile variable in java?
Ans: Volatile is used to indicate that a variable’s value will be modified by different threads.
22. What is Join() method?
Ans: The join() method waits for a thread to die. In other words, we can say, it causes the currently running threads to stop executing until the thread it joins with completes its task.
23.What is the CurrentThread method?
Ans: The currentThread() method returns a reference to the currently executing or running thread object. public void run(){ System.out.println( Thread.currentThread().getName()); //it will give you a name of currently executing thread.
24.What is Daemon Thread?
Ans: Daemon thread is a service provider thread that provides services to the user thread. Its life depends on the mercy of user threads i.e. when all the user threads dies, JVM terminates this thread automatic There are many java daemon threads running automatically. GC and finalizer are examples of daemon thread. You can see all the detail by typing the jconsole in the command prompt. The jconsole gives information about the loaded classes, running threads, memory usage etc.
1.What is Collection? What is Collections Framework?
Ans: A collection is an object that groups multiple elements into a single unit. collections Framework: Collections framework provides an architecture for manipulating and representing collections.
2. What is the root interface in the collection hierarchy?
Ans: The root interface in the collection hierarchy is the Collection interface.
3.What is the difference between Collection and Collections?
Ans: The collection is an interface while Collections is a java class, both are present in java. util package.
4.Which collection classes are synchronized or thread-safe?
Ans: Stack, Properties, Vector and Hashtable are synchronized classes (thread-safe).
5.What is the difference between ArrayList and Vector?
Ans: ArrayList is not synchronized.
ArrayList is not a legacy class.
Vector is a legacy class. Vector is synchronized.
6. What is the difference between ArrayList and Linked List?
Ans: ArrayList uses a dynamic array ArrayList is not good for manipulation because a lot of shifting is required.ArrayList is better to store and get data.
7. What is the difference between ArrayList and Linked List?
Ans: ArrayList uses a dynamic array ArrayList is not good for manipulation because a lot of shifting is required.ArrayList is better to store and get data. Linked List uses a doubly LinkedList. Linked List is good for manipulation. It is better to manipulate data.
8.What is the difference between List and Set?
Ans: Set contains only unique elements while List can contain duplicate elements. Set stores elements in an unordered way while List is ordered. List maintains the order in which the objects are added.
9. What is the difference between Map and Set?
Ans: Map object has unique keys each contains a value, while Set contain only unique values.
10. What is an iterator?
Ans: Iterator is an interface. It is present in java.util package. It gives methods to iterate over any Collection.
11. What is the difference between Iterator and Enumeration?
Ans: The main difference between the Iterator and Enumeration is that Iterator has remove() method while Enumeration doesn’t . Using Iterator we can manipulate objects by adding and removing the objects from the collections. Enumeration can only traverse the objects and fetch the objects.
12.What is the difference between Queue and Stack?
Ans: The queue is a data structure that follow FIFO (first in first out) property. An example of a Queue is buying tickets in cinema theatres. Stack is a data structure that follows the LIFO (last in first out) property. An example of a Stack is the insertion or removal of CD from the CD case.
13.How to reverse the List in Collections?
Ans: Collections.reverse(listobject);
14. What is the difference between HashMap and Hashtable?
Ans: HashMap allows one null key and any number of null values while Hashtable does not allow null keys and null values. HashMap is not synchronized or thread-safe while Hashtable is synchronized or thread-safe.
15.What is the difference between Array and ArrayList in Java?
Ans: The array is static in size while ArrayList is dynamic in size. The array can contain primitive data types while ArrayList can not contain primitive data types.
16. What is the difference between HashSet and TreeSet?
Ans: HashSet maintains the inserted elements in random order while TreeSet maintains elements in the sorted order HashSet can store null objects while TreeSet can not store null objects.
17. How will you make Collections readOnly?
Ans: Collections.unmodifiab leCollection (Collection c)
18. What is the advantage of the generic collection?
Ans: If you use a generic class, you don’t need typecasting. It is typesafe and checked at compile time.
19.What is the Dictionary class?
Ans: Dictionary class provides the capability to store key-value pairs.
1. What is an Exception in Java?
Ans: The exception is an error that can come during the execution of a program and stop its normal flow. An exception can be of different types such as wrong data entered by the user, hardware failure, network connection failure etc.
2.What are the Exception Handling Keywords in Java?
Ans: There are four keywords used in java exception handling.
1. throw.
2. throws.
3. try-catch.
4. finally.
3. What is the difference between Checked Exception and Unchecked Exception?
Ans: Checked Exception: Checked exceptions are checked at Unchecked Exception: Unchecked exceptions are not checked at compile-time.It comes at runtime. compile-time.
4.What is the base class for Error and Exception?
Ans: Throwable.
5. Is it necessary that each try block must be followed by a catch block?
Ans: It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block or a final block.
6. What is Finally block?
Ans: Finally block is a block that is always executed.
7. Can finally block be used without catch?
Ans: Yes, by try block. finally, the block must be followed by either try or catch.
8. Is there any case when finally will not be executed?
Ans: The finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error.
9. What is difference between throw and throws?
Ans: The throw is used to explicitly throw an exception. Throws is used to declare an exception. The throw is followed by an instance. Throws is followed by class.
10. What is OutOfMemory Error in Java?
Ans: OutOfMemoryError in Java is a subclass of java.lang.Virtual MachineError and it’s thrown by JVM when it ran out of heap memory. We can solve this error by giving more memory to run the java program.
11.What is OutOfMemoryError in Java?
Ans: OutOfMemoryError in Java is a subclass of java.lang.Virtual MachineError and it’s thrown by JVM when it ran out of heap memory. We can solve this error by giving more memory to run the java program.
12.Can we have an empty catch block?
Ans: We can have an empty catch block, but if we use it in our program, and if an exception occurs at this point then how can we know that the exception occurs at this point so
that we don’t use empty catch block.
Prepare For Your Placements: https://lastmomenttuitions.com/courses/placement-preparation/
/ Youtube Channel: https://www.youtube.com/channel/UCGFNZxMqKLsqWERX_N2f08Q
Follow For Latest Updates, Study Tips & More Content!
Not a member yet? Register now
Are you a member? Login now