Skip to main content

Posts

Showing posts from August, 2021

Enhancing Indoor Air Quality: A Guide to Better Health and Comfort

In today's world, where we spend a significant amount of our time indoors, the quality of the air we breathe inside our homes and workplaces is crucial for our health and well-being. Poor indoor air quality (IAQ) can lead to various health issues, including allergies, respiratory problems, and even long-term conditions. This blog post explores effective strategies for managing and improving indoor air quality. Understanding Indoor Air Pollutants Indoor air pollutants can originate from various sources: Biological Pollutants: Mold, dust mites, and pet dander. Chemical Pollutants: Volatile organic compounds (VOCs) from paints, cleaners, and furnishings. Particulate Matter: Dust, pollen, and smoke particles. Strategies for Improving Indoor Air Quality Ventilation: Natural Ventilation: Open windows and doors regularly to allow fresh air circulation. Mechanical Ventilation: Use exhaust fans in kitchens and bathrooms to remove pollutants directly at the source. Air Purifiers: HEPA Filt

Java important question - 13

What are the ways to initialize a final variable ?           We must initialize a final variable, otherwise compiler will throw compile time error. A final variable can only be initialize once, either via an initializer or an assignment statement. There are three ways to initialize a final variable, 1) You can initialize a final variable when it is declared. This approach is most common. A final variable is called blank final variable, if it is not initialized while declaration. Below are the two ways to initialize a blank final variable. 2) A blank final variable can be initialized inside constructor. If you have more than one constructor in your class then it must be initialized in all of them, otherwise compile time error will be thrown. 3) A blank final static variable can be initialized inside static block. What is the difference between transient and volatile variable in Java ? Transient:           In Java, it is used to specify the variable is not being serialized. Serialization

Java important question - 12

What is the use of final keyword in java ?           final keyword is used in different contexts. First of all, final is a non-access modifier applicable only to a variable, method or a class. Following are different contexts where final is used, a) Final variable -  To create constant variable. When a variable is declared with final keyword, it's value can't be modified, essentially a constant. This also means that you must initialize a final variable. If the final variable is reference this means that the variable cannot be rebound to reference another object, but the internal state of the object pointed by that reference variable can be changed. i.e. you can add or remove elements from final array or final collection. It is good practice to represent final variables in all uppercase, using underscore to separate words. Java interface variables are by default final and static. b) Final methods - Prevent method overridding. We can use the final keyword with methods to make sur

Java important question - 11

What is the difference between creating String as new() and literal ?           When we create String with new() operator, it's created in heap and not added into string pool. When String created using literal are created in String pool itself which exist in permgem area of heap.  String s = new String("Test");           This does not put the object in string pool, we need to call String.intern() method which is used to put them into String pool explicitly. When you create String object as String literal. Eg : String s = "Test" java automatically put that into String pool. Define a StringJoiner and write sample code ?           StringJoiner is a utility method to construct a string with the desired delimiter. StringJoiner strJoiner = new StringJoiner                                                                  ("."); strJoiner.add("AAA").add("BBB"); System.out.Println(strJoiner); Output:  AAA.BBB Are arrays of primitive data typ

Java important question - 10

Can we use String with switch case ?           One of the java7 features was improvement of switch case to allow strings. So if you are using java 7 or higher version, you can use String in switch case statement. private   static void printColorUsingSwitch                                                (String color) { switch(color) { case "blue":      System.out.println("Blue");      break; case "red":      System.out.println("Red");      break; default :      System.out.println("Invalid color                                                code"); } Q) Difference between String, StringBuilder and StringBuffer in java String :           String is immutable object. The object created as a string is stored in the Constant string pool. Every immutable object in Java is thread safe, that implies string is also thread safe. String cannot be used by two threads simultaneously.            String once assigned cannot be changed. String demo = &

Java important Question - 9

Q) Can we declare a class as static ?           We can't declare a top-level class as static however an inner class can be declared as static. If inner class is declared as static,it's called  static nested class. The static nested class is same as any other top-level class and is nested for only packaging convenience. Also when we declare a class as 'static' then it can be referenced without the use of an object. Q) What is static import ?           static import is a feature introduced in java version 5.0 that allows members defined in a class as public static to be used in Java code without specifying the class in which the field is defined. If we have to use any static variable or method from other class, usually we import the class and then use the method/variable with class name. We can do the same thing by importing the static method or variable only and then use it in the class as if it belong to it. import static java.lang.System.*; class StaticImportExample {

Java important question - 8

Q) What is composition in Java ?           Composition is the design technique to implement has a relationship in classes. We can use object composition for code reuse.           Java composition is achieved by using instance variables that refer to other objects. The benefit of using composition is that we can control the visibility of other objects to client classes and reuse only what we need. Q) What is the benefits of composition over inheritance ?           Prefer composition over inheritance is a one of the popular object oriented design principles, some of the possible reasons are 1) Inheritance is tightly coupled whereas composition is loosely coupled. 2) Any change in superclass might affect subclass even though we might not be using the superclass methods. For example, if we have a method test() in the subclass and suddenly somebody introduces a method test() in the superclass, we may get compilation errors in the subclass. Composition will never face this issue because we a

Java important question - 7

Q) What is Inheritance ?           Inheritance is one of the key features of object oriented programming. It is the mechanism in java in which one class inherits the properties (methods and fields) of another class.           The class which inherits the properties of other is known as subclass and the class whose properties are inherited is known as superclass.           extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword. Syntax: class Super { .......... .......... } class Sub extends Super { .......... .......... } Q) Can an interface implement or extend another interface ?          Interface can't implement  another interface, since interfaces can't have method implementations. An interface can extend another interface. Q) What is Marker interface ?         A Marker interface is an empty interface without any method but used to force some functionality in implementing classes by java. Some of the well known marker int

Java important question - 6

Q) What is the difference between abstract class and interface ? Answer : 1) The abstract keyword is used to declare  abstract class.  The interface keyword is used to declare interface. 2) Abstract class have all the features of a normal java class except that we can't instantiate it. We can use abstract keyword to make a class abstract but interfaces are a completely different type and can have only public static final constants and method declarations. 3) Abstract class can have abstract and non abstract methods. Interface can have only abstract methods. 4) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables. 5) A java abstract class can have class members like private, protected,etc. Members of a java interface are public by default. 6) Abstract classes can have constructors but interface can't have constructors. 7) A subclass can extend only one abstract class but it can implement multiple interfaces. 8)

Java important Question - 5

Q) What are wrapper classes? Why do we need wrapper classes ? Answer :           Wrapper class provides the mechanism to convert primitive into object and object into primitive. In the java.lang package java provides a separate class for each of the primitive data types namely Boolean, Byte, Short, Integer, Float, Long, Double & Character. These are known as wrapper classes because they wrap the primitive data type into an object of the class. Need of wrapper classes : * They convert primitive data types into objects. Objects are needed if we wish to modify the arguments passed into a method. * The classes in java.util package handles only objects and hence wrapper classes help in this case also. * Data structures in the collection framework, such as ArrayList and vector, store only objects and not primitive types. * An object is needed to support synchronization in multithreading. Q) What is an interface ? Answer :           An interface is a reference type in Java. It is similar