JAVA IQ

¡Supera tus tareas y exámenes ahora con Quizwiz!

Difference between ArrayList vs LinkedList

● ArrayList internally uses a dynamic array to store the elements. ● Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the bits are shifted in memory. ● ArrayList is better for storing and accessing data. ● LinkedList internally uses a doubly linked list to store the elements (consist of value + pointer to previous node and pointer to the next node) ● Manipulation with LinkedList is faster than ArrayList because it uses a doubly linked list, so no bit shifting is required in memory. ● LinkedList is better for manipulating data.

Difference between array and Arraylist?

● Arrays are fixed in size but ArrayLists are dynamic in size. ● Array can contain both primitives and objects but ArrayList can contain only object elements. ● To find the size on an Array we use ArrayName.length and for arrayList we use ArrayListName.size() ● Array uses assignment operators to store elements but ArrayList uses add() to insert elements. ● Array can be multi dimensional , while ArrayList is always single dimensional.

What is the difference between constructor and method?

● Constructor must not have a return type whereas methods must have a return type. ● Constructor name is the same as the class name where the method may or may not be the same class name. ● Constructor will be called automatically whenever an object is created whereas the method invokes explicitly. ● Compiler provides a default constructor when no constructor is created whereas the compiler doesn't create any methods.

Types Of Inheritance In Java

● Single Inheritance - single base class and single derived class. ● Hierarchical Inheritance - when a class has more than one child classes (sub-classes) ● Multilevel Inheritance - single base class, single derived class and multiple intermediate base classes. ● Multiple Inheritance - multiple classes and single derived class (Possible through interface only) ● Hybrid Inheritance - combination of both Single and Multiple Inheritance (Possible through interface only)

What is inheritance and benefits of it?

● The process of acquiring properties (variables) &methods (behaviors) from one class to another class is called inheritance. ● We are achieving the inheritance concept by using extends keyword. Also known as is-a relationship. ● Extends keyword is providing a relationship between two classes. ● The main objective of inheritance is code extensibility whenever we are extending the class automatically code is reused.

this vs this()?

"this" refers to the current object instance in a class, while "this()" is a constructor call that calls another constructor in the same class.

Method overloading & overriding?

- Method overloading = multiple methods with same name in a class, different signatures. - Method overriding = subclass has a method with same name and signature as parent class.

Explain public static void main (String args[])?

public: is an access modifier that makes it accessible by any Class. static: is a keyword to call this method directly using class name without creating an object of it. void: it is a return type that does not return any value. main(): it is the name of the method. string args[]: it's a command line argument passed to the main method. method that takes an array of strings as input arguments and is the entry point for a Java program.

When to use abstract class and interface in Java?

● An abstract class is good if you plan on using inheritance since it provides a common base class implementation to derived classes. ● An abstract class is also good if you want to be able to declare non-public members. In an interface, all methods must be public.

What is the parent of all exceptions?

Throwable class is parent of all Exceptions.

What is an instance variable and how do you use it?

Variables which are declared inside the class, but outside a method, constructor or any block of code. Instance variables are created with the use of the keyword 'new'.

Versions of java you worked with?

Version Release Date Java SE 7 July 2011 Java SE 8 March 2014 Java SE 9 September 2017 Java SE 10 March 2018 Java SE 11 September 2018

Can we overload a constructor?

WE CAN OVERLOAD CONSTRUCTOR (using different number or type of parameters)

Can super() and this() keywords be in the same constructor?

We can use super() and this() only in the constructor and they have to be in the first line of code, for that reason we CANNOT use them within the same constructor.

Can we override/overload the main method? Explain the reason?

We cannot override a static method, so we cannot override the main method. However, you can overload the main method in Java. But the program doesn't execute the overloaded main method when you run your program Practically I do not see any use of it and we don't use it in my framework.

Why do we need the main method in java?

We need the main method in Java to provide an entry point for the JVM to execute the program. The main method is necessary in Java to provide the starting point for the program's execution.

Any example or practical usage of Run time polymorphism?

WebDriver webdriver=new ChromeDriver()

Do we need to have a main method in java?

Yes, if you want the program to be executed, But it is not mandatory, without main() method our Java code will compile but won't run.

Can we achieve 100% abstraction in JAVA?

Yes, with the help of Abstract Classes and Interfaces. Using abstract class we can achieve 0 to 100% or partial abstraction. Using interfaces we can achieve 100% or full abstraction.

How did you use Map in your framework?

Ask Chat GPT

How do you use Method overloading & overriding in your framework?

Ask Chat GPT

ArrayList vs HashSet?

Both ArrayList and HashSet are non synchronized collection class Both ArrayList and HashSet can be traversed using Iterator ● ArrayList implements List interface ● ArrayList allows duplicate values ● ArrayList maintains the order of the object in which they are inserted ● In ArrayList we can add any number of null values ● ArrayList is index based ● HashSet implements Set interface ● HashSet doesn't allow duplicates values ● HashSet is an unordered collection and doesn't maintain any order ● HashSet allow one null value ● HashSet is completely object based

StringBuffer vs StringBuilder

Both Classes are mutable, except StringBuffer is thread-safe (synchronized) and StringBuilder is not thread-safe (non synchronized) which makes StringBuilder faster compared to StringBuffer.

What is the difference between HashTable vs HashMap ?

Both HashMap and Hashtable implement Map Interface ● HashMap is non synchronized, so it is not-thread safe ● HashMap is fast ● HashMap allows one null key and multiple null values ● Hashtable is synchronized, so it is thread-safe ● Hashtable is slow ● Hashtable doesn't allow any null key or value

Super vs super()?

Both are used in a subclass as a way to invoke or refer to its superclass. super keyword is used to call super class super() is used to call super class constructor from subclass constructor.

ArrayList vs Vector

Both implement List Interface and maintaining the insertion order. ArrayList is not synchronized, so it is fast. Vector - is synchronized, so it is slow.

How can we access variables without creating an object instance of it

By making them static.

What is polymorphism?

Combination of overloading and overriding is known as Polymorphism. Polymorphism is the ability of an object to take on many forms. Polymorphism allows us to perform a task in multiple ways.

Write a java program to check whether a given number is prime or not?

int given = 10; boolean isPrime = true ; if ( given >1) { for ( int i =2; i < given ; i ++) { if ( given % i ==0) { isPrime = false ; break ; }} } else { isPrime = false ; } System. out .println( "Given number " + given + " is prime? " + isPrime );

Types of exceptions you faced in your project?

1. Checked Exception - are the exceptions that are checked at compile time. Example of checked exceptions: ● ClassNotFoundException - Class not found ● InstantiationException - Attempt to create an object of an abstract class or interface ● FileNotFoundException - Attempt to open file that doesn't exist or open file to write but have only read permission 2. Unchecked Exception - are the exceptions that are not checked at compile time, they are Runtime Exceptions. Exception faced as part of java perspective: ● ArithmeticException - Arithmetic error, such as divide-by-zero. ● ArrayIndexOutOfBoundsException - Array index is out-of-bounds. ● NullPointerException - Invalid use of a null reference. ● IllegalArgumentException - Illegal argument used to invoke a method.

Types of polymorphisms

1. Compile time polymorphism (Static binding) - Method overloading 2. Runtime polymorphism (Dynamic binding) - Method overriding

What is the difference between HashSet vs HashMap ?

1. HashSet class implements Set interface 2. In HashSet, we store objects(elements or values). 3. HashSet does not allow duplicate elements that mean you cannot store duplicate values in HashSet. 4. HashSet permits to have a single null value. 5. HashSet is not synchronized. 1. HashMap class implements the Map interface 2. HashMap is used for storing Key, Value paired objects. 3. HashMap does not allow duplicate keys however it allows having duplicate values. 4. HashMap permits a single null key and any number of null values. 5. HashMap is not synchronized.

Difference between == and =.

= we are assigning value == comparison operator

What is a constructor?

A block of code similar to a method used to initialize objects of that class when they are created.

Which catch block will get executed if you get ArithmeticException?

A catch block that has an ArithmeticException parameter or a superclass of ArithmeticException (such as Exception) will get executed.

How did you use Access Modifiers (Private,public,protected)?

Access levels In front of classes, variables, methods and constructors. Access modifier cannot be applied for local variables.

What are Access Modifiers (Private,public,protected)?

Access levels for classes, variables, methods and constructors. public: Can be accessed from outside the package. protected: Can be accessed in the same package or any subclass of the same package or different package. default: When no access modifier is specified for a class. private: Can be accessed only within the class

How can you handle exceptions?

An Exception is a problem that can occur during the normal flow of execution. Depending on the situation, we can use try and catch blocks. In try block: Code that might throw some exceptions In catch block: We define exception type to be caught and what to do if an exception happens in TRY block code

Difference between an abstract class and interface?

An abstract class can have both abstract and non-abstract methods and can have instance variables, while an interface can only have abstract methods and constants (static final variables). A class can implement multiple interfaces but can only inherit from one abstract class.

What is array and Arraylist (List)? Difference between them?

An array is a container object that holds a fixed number of values. The length of an array is established when the array is created. After creation, its length is fixed. ArrayList is a class which implements the List interface of collection framework. ArrayList is a dynamic data structure, we can add and remove the elements from ArrayList and size will be adjusted automatically.

Difference between Instance Variable and static Variable?

An instance variable is specific to each instance of a class, while a static variable is shared across all instances of the class.

ArrayList vs LinkedList

ArrayList and LinkedList, both implement List interface and provide capability to store and get objects as in ordered collections. Both are non synchronized classes and both allow duplicate elements.

How can you remove all duplicates from ArrayList?

ArrayList<String> aList = new ArrayList<>(); aList .add( "John" ); aList .add( "Jane" ); aList .add( "James" ); aList .add( "Jasmine" ); aList .add( "Jane" ); aList .add( "James" ); // 1 way HashSet<String> set = new HashSet<>( aList ); System. out .println( set );

What is encapsulation?

Encapsulation is the practice of hiding the internal details of an object from the outside world and providing a public interface for interacting with the object. This helps to ensure the integrity of the object's state and behavior, and allows for easier maintenance and modification of the code.

What is the difference between final,finally and finalize?

Final keyword: ● Used to apply restrictions on class, methods, and variables. ● Used to declare constant values. The variable declared as final should be initialized only once and cannot be changed. ● Used to prevent inheritance. Java classes declared as final cannot be extended. ● Used to prevent method overriding. Methods declared as final cannot be overridden. Finally block : ● The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. Finalize() method : ● finalize() is a protected method of java.lang.object class and it is inherited by every class we create in java and it means that this method is available in all objects that we create in java. ● finalize() method is used to perform some clean up operations on an object before it is removed from memory.

How many catch blocks can we have?

There can be any number of catch blocks for a single try block, it should be followed by either a catch block or a finally block. However only the catch block encountered first on the call stack that satisfies the condition for the exception will be executed for that particular exception, rest will be ignored.

What is mutable and immutable?

Immutable means once we are creating String objects it is not possible to perform modifications on existing objects. (String object is fixed object) mutable means once we are creating object it is possible to perform modification.

How to make a call to the Garbage Collector?

In most programming languages, you don't need to manually call the garbage collector, as it is invoked automatically by the runtime environment.

Where did you use static in your framework?

In our utility package we have a class where we store common static methods, such as wait, switch between frames, clicking on buttons, selecting values from drop down.

Explain OOPS concepts?

Inheritance, polymorphism, abstraction and encapsulation. - Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent object. It provides code reusability. - Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in Java is when a parent class reference type of variable is used to refer to a child class object. - Abstraction is a process of hiding the implementation of internal details and showing the functionality to the users. Abstraction lets you focus on what the object does instead of how it does it. - Encapsulation is a mechanism of binding code and data together in a single unit. We can hide direct access to data by using a private key and we can access private data by using getter and setter methods.

What is a static keyword in java?

Is used to create class-level variables and methods that can be accessed without creating an instance of the class.

What is a method?

It is a block of code that can be called to perform a specific task, and can be called (invoked) at any point in a program by the method's name.

Difference between JRE, JDK ?

JRE is for running Java programs, while JDK is for developing Java programs. JVM Java Virtual machine . It translates and executes the Java bytecode.

What version of java do you currently use in your framework?

Java SE 11

What is collection in Java and what type of collections have you used?

Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit. Maps are not part of collection but built based on the collection concepts' Mostly in my current project we use List and Map.

What is the difference between local and instance variables?

Local variables are declared inside a method or constructor or blocks of code. Instance variables are declared inside the class, but outside a method, constructor or any block of code.

What is a Map?

Map is a collection of unique key and value pairs. Each key and value pair is known as an entry. A Map is useful if we have to search, update or delete elements on the basis of a key. HashMap doesn't maintain the insertion order of elements. LinkedHashMap maintains the insertion order of elements. TreeMap orders its elements based on their values and is slower than HashMap. Hashtable is similar to HashMap except it is synchronized or we say thread safe.

Can you make the constructor static?

NO. Constructors cannot be abstract, final, static. Rules to create constructor: 1. Constructor name class name must be the same. 2. Constructor do not have any return type. 3. Constructor may or may not have parameters.

Is Java 100% Object-oriented?

No, Java is not 100% object oriented , since it has primitive data types, which are different from objects.

Can we create an object for an abstract class? interface?

No, we cannot create an object for an abstract class or an interface. We can only create objects of concrete (non-abstract) classes that extend the abstract class or implement the interface.

Can we overload and override private methods?

No. It is not possible to override private methods because these methods are specific to classes, not visible in child classes. Yes, It is possible to overload private methods in Java, but only within the same class.

Can you override static methods?

No. Static methods are bound with class it is not possible to override static methods.

What are the primitives and wrapper classes?

Primitives are data types in Java: byte, short, int, long, float, double, char, boolean. Every primitive data type has a class dedicated to it and these are known as wrapper classes.

What is singleton and have used the singleton concept in your project ?

Singleton is a design pattern that allows only one instance of a class to exist throughout the lifecycle of an application. ● Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the Java virtual machine. ● The singleton class must provide a global access point to get the instance of the class. ● Singleton pattern is used for logging , drivers objects In my current project we use a singleton concept for our page classes where we store page elements.

What is the main method?

Starts execution of any java program. Entry point It is a block of code that can be called to perform a specific task, and can be called (invoked) at any point in a program by the method's name.

Write a java program to reverse String?

String a = "Hello Syntax" ; StringBuffer sb = new StringBuffer( a ); System. out .println( sb .reverse());

Find the number of words in string?

String myString = "Today is Wednesday and it is Java Class" ; String[] array = myString .split( " " ); int words = array . length ; System. out .println( "Total words in string:" + myString + " = " + words );

What is the difference between String and StringBuffer?

String object is immutable and StringBuffer object is mutable. 2 ways to make a String mutable is by using StringBuffer or StringBuilder .

What is the difference between String and StringBuilder?

String object is immutable and StringBuilder object is mutable. 2 ways to make String mutable by using StringBuffer or StringBuilder .

Write a Java Program to find whether a String is a palindrome or not.

String original = "hellhe" ; String reverse = "" ; for ( int i = original .length()-1; i >=0; i --) { reverse = reverse + original .charAt( i ); } if ( original .equals( reverse )) { System. out .println( "Given String is Palindrome" ); } else { System. out .println( "Given String is NOT Palindrome" ); }

Find out how many alpha characters are present in a string?

String str = " Hih9898jhjh%%^$%^ oio " ; str = str .replaceAll( "[^A-Za-z]" , "" ); int number = str .length(); System. out .println( "Number of alpha character = " + number );

Reverse a string word by word?

String str = "Todays is Sunday" ; String reversed = "" ; String[] array = str .split( " " ); for ( int i = array . length - 1; i >= 0; i --) { reversed = reversed + array [ i ]+ " " ; } System. out .println( reversed );

Swap 2 strings without a temporary variable?

String x = "Hello" ; String y = "Welcome" ; x = x + y ; //HelloWelcome y = x .substring(0,( x ).length()- y .length()); x = x .substring( y .length()); System. out .println( x ); System. out .println( y );

What is a garbage collector?

The garbage collector is a long running thread (daemon thread) which gets rid of objects that no longer hold a reference in the heap to free up space. A garbage collector is a software component that automatically frees up memory used by programs that are no longer needed.

Use of constructor in class?

The primary use of constructor is to initialize the instance variables.

What is the difference between throw and throws?

The throw keyword is used to explicitly raise a exception within the program. On the contrary, the throws clause is used to indicate those exceptions that are not handled by a method. Each method must explicitly specify which exceptions does not handle, so the callers of that method can guard against possible exceptions. Finally, multiple exceptions are separated by a comma. Throws : ● is used to declare an exception, which means it works similar to the try-catch block. ● is used in method declaration. ● is followed by exception class names. ● you can declare multiple exception with throws ● throws declare at method it might throws Exception ● used to handover the responsibility of handling the exception occurred in the method to the caller method. Throw : ● is used in the method body to throw an exception ● throw is followed by an instance variable ● you cannot declare multiple exceptions with throw ● The throw keyword is used to handover the instance of the exception created by the programmer to the JVM manually. ● throw keyword is mainly used to throw custom exceptions.

How do you use inheritance in your code/Framework?

You can use inheritance to create a subclass that inherits the properties and methods of a parent class. In our current Framework we have BaseClass where we initialize the WebDriver interface. And after we extend the Base Class in other classes such as PageInitializer and to the Common methods where we have functions to work with Web Browser.

Write a java program to find the second largest number in the array?

int [] numbers = { 32, 61, 16, 89, 74, 25 }; Arrays .sort( numbers ); System. out .println( "Second Largest " + numbers [ numbers . length - 2]);

Write a java program to find Maximum and minimum number in the array?

int [] numbers = { 32, 61, 16, 89, 74, 25 }; Arrays .sort( numbers ); System. out .println( "Smallest " + numbers [0]); System. out .println( "Biggest " + numbers [ numbers . length - 1]);

Write a Java Program to print the first 10 numbers of Fibonacci series.

int a , b , c ; a =0; b =1; for ( int i =1; i <=10; i ++) { System. out .print( a + " " ); c = a + b ; a = b ; b = c ; }

Write a program to swap 2 numbers without a temporary variable?

int a = 10; int b = 20; a = a + b ; // first this should be there a=10+20=30 b = a - b ; // b= 30-20=10 a = a - b ; // a=30-10=20 System. out .println( a ); System. out .println( b );


Conjuntos de estudio relacionados

Module 8: Klepper, "Sexual Exploitation and the Value of Persons"

View Set

Everfi Financial LIteracy- Module 5 - Higher Education - Final Quiz Questions and Answers

View Set

The study of life reveals unifying themes

View Set

The Skeletal, Muscular, and Nervous Systems

View Set