Last Updated on February 22, 2023 by Prepbytes
Infosys is a global leader in technology services and consulting, with a diverse range of projects and clients across industries. Working at Infosys offers many perks and benefits for employees. Founded in 1981, the company has grown to become one of the largest IT services companies in the world. Infosys is a well-established and highly respected company in the IT industry, and it has a strong reputation for delivering high-quality services and innovative solutions. Infosys is committed to the professional development of its employees, and it provides ongoing training and career advancement opportunities. Whether you are just starting out in your career or looking to take the next step, Infosys is an excellent choice for those who are looking to grow and develop professionally.
In this article, we have compiled the Java Questions that were asked in interviews before. So, let us start.
Infosys Java Language Questions: Fundamentals
Here are the questions that were previously asked in the interviews at Infosys.
Q1. What are List, Set, and Map in Java?
Ans. List, Set, and Map are all data structures in programming that are used to store collections of values or objects. However, they differ in their properties and the way they store and manage data.
- List: A List is an ordered collection of elements that allows duplicates. Lists can be implemented as arrays or linked lists and are useful for storing collections of items that need to be maintained in a specific order.
- Set: A Set, on the other hand, is an unordered collection of unique elements. Sets can be implemented as hash sets or trees and are useful for representing collections of elements where the uniqueness of elements is important and order is not.
- Map: A Map is a collection of key-value pairs where each key is unique. Maps can be implemented as hash maps or trees and are useful for storing collections of values where each value is associated with a specific key. The key is used to retrieve the value, so maps are often used to implement dictionaries or lookups.
Q2. Which classes in Java support mutable Strings?
Ans. In Java, the StringBuilder and StringBuffer classes support mutable strings. These classes provide a convenient and efficient way to manipulate strings in memory, as they allow for operations such as appending, inserting, and deleting characters from a string without creating a new string object each time.
The StringBuilder class is not thread-safe, while the StringBuffer class is thread-safe. Therefore, if you are working with multiple threads and need to ensure that your mutable string operations are synchronized, you should use StringBuffer. Otherwise, if you are working in a single-threaded environment, StringBuilder is typically the preferred choice due to its higher performance.
Q3. Does Java8 support Multiple Interfaces?
Ans. Yes, Java 8 and later versions support multiple interfaces. In Java, a class can implement multiple interfaces by separating the interface names with commas in the implements clause of the class definition. For example
public class MyClass implements Interface1, Interface2 {
// class body
}
Here, MyClass implements both Interface1 and Interface2. This feature was available in previous versions of Java as well, and it continues to be supported in Java 8 and later versions.
Q4. What do you mean by String Immutability in Java?
Ans. In Java, a string is immutable, which means that once a string object is created, its value cannot be changed. If you want to modify a string, you need to create a new string object with the desired value. This is different from some other programming languages where strings can be modified directly. The immutability of strings in Java is enforced by making the string class final and ensuring that its instance variables (i.e., the characters in the string) are declared as private final.
Q5. What is OOP and what are its advantages?
Ans. OOP stands for Object-Oriented Programming. It is a programming paradigm that emphasizes the use of objects and classes to represent real-world entities and concepts. In OOP, a program is made up of objects that interact with each other to perform specific tasks.
Here are some advantages of using OOP:
- Modularity: OOP allows for the creation of self-contained and modular programs. This makes it easier to maintain and modify code, as changes can be made to one part of the program without affecting the rest of the program.
- Reusability: OOP enables the creation of reusable code. Once a class is created, it can be used by other parts of the program or by other programs entirely. This can save time and effort in development.
- Encapsulation: OOP allows for encapsulation, which means that data and methods are bundled together in a single unit, such as a class. This provides a level of abstraction, which makes it easier to manage complexity and prevents unwanted changes to the data.
- Inheritance: OOP supports inheritance, which allows new classes to be based on existing classes. This enables the creation of more specialized classes that inherit the properties and methods of the base class. This can save time and effort in development.
- Polymorphism: OOP supports polymorphism, which allows different objects to be treated as if they were of the same type. This enables the creation of flexible and extensible programs.
Overall, OOP provides a structured approach to programming that can make code more modular, reusable, and easier to maintain and modify.
Q6. What is Weak Hash Map?
Ans. Weak Hash Map is a class in Java that is a type of Map that allows the garbage collector to remove keys from the map if they are no longer referenced elsewhere in the program. This is different from a regular HashMap, where keys remain in the map until they are explicitly removed, even if they are no longer needed.
In a WeakHashMap, each key is stored as a weak reference, which means that it may be garbage collected when no other references to the key exist. This is useful in situations where the keys are not needed anymore and you want to free up memory.
It’s worth noting that because keys in a WeakHashMap can be garbage collected, the map may not always be consistent or predictable in its behavior, since it may be modified by the garbage collector in ways that are difficult to anticipate.
Q7. What are Static Overloading and Dynamic Overloading in Java?
Ans. In Java, overloading refers to the practice of defining multiple methods with the same name but different parameters. There are two types of overloading in Java:
- Static Overloading: Static overloading is also called compile-time overloading because the decision of which method to call is made at compile-time based on the parameters passed to the method. In other words, the method signature is used to determine which method to call. This means that the static overloading is resolved at compile-time and is independent of the object being referred to.
- Dynamic Overloading: Dynamic overloading is also called runtime overloading because the decision of which method to call is made at runtime based on the object being referred to. In other words, the type of the object determines which method to call. This means that dynamic overloading is resolved at runtime and is dependent on the object being referred to. Dynamic overloading is used in polymorphism, where objects of different classes can be referred to using a common interface, and the appropriate method is called based on the type of the object being referred to.
Q8. State the Differences Between an Array and an ArrayList?
Ans. Here are some differences between an array and an ArrayList in Java:
- Size: Arrays have a fixed size that is determined when they are created, and this size cannot be changed. ArrayLists, on the other hand, can grow and shrink dynamically as elements are added and removed.
- Type: Arrays can store primitive data types, as well as objects of any class. ArrayLists can only store objects, not primitives.
- Memory allocation: Arrays are stored in contiguous memory locations, while ArrayLists are implemented using an underlying array that can be resized as needed.
- Performance: Arrays are generally faster when accessing elements since they use direct indexing. ArrayLists, on the other hand, are slower when accessing elements, since they use an iterator or the get() method to access elements.
- Length: The length of an array is defined using the length attribute, whereas the length of an ArrayList is defined using the size() method.
Q9. Enlist the Differences between String and StringBuffer.
Ans. Here are some differences between String and StringBuffer in Java:
- Immutability: String objects are immutable, which means that once a string object is created, its value cannot be changed. StringBuffer objects are mutable, which means that you can modify the value of the object after it’s created.
- Thread-safety: String objects are thread-safe, which means that they can be accessed by multiple threads at the same time without any issues. StringBuffer objects, on the other hand, are not thread-safe, but there is a thread-safe version called StringBuilder.
- Performance: Because String objects are immutable, creating a new String object each time you modify it can be inefficient. StringBuffer objects, on the other hand, can be modified without creating a new object each time, making them more efficient in certain situations.
- Usability: Strings are generally used to represent constants, whereas StringBuffer is used to represent mutable sequences of characters.
- Methods: String has limited methods to modify its content, such as substring, replace, and concat. StringBuffer provides a variety of methods to modify its content, such as append, insert, delete, and replace.
Overall, if you need to modify a string frequently or if performance is a concern, then you should use StringBuffer or StringBuilder. If you need to represent a constant value, then you should use String.
Q10. What Types of Inheritance are supported in Java?
Ans. In Java, there are five types of inheritance that are supported:
- Single Inheritance: A subclass extends a single superclass.
- Multilevel Inheritance: A subclass extends a superclass, which in turn extends another superclass.
- Hierarchical Inheritance: Multiple subclasses extend a single superclass.
- Multiple Inheritance (through interfaces): A subclass implements multiple interfaces, which define the behaviors that the subclass must implement.
- Hybrid Inheritance: This is a combination of any two or more types of inheritance mentioned above.
However, it’s worth noting that Java doesn’t support multiple inheritance of classes, only interfaces, to avoid the "diamond problem".
Infosys Java Language Questions: Coding
In this section, we will discuss Infosys Java Language Questions. There is a higher probability that you will see these questions in your interview.
Q1. Write a Java program to Rotate a Matrix by 90 degrees.
Ans.
class Main { static int N = 4; public static void main (String[] args){ int arr[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; //Rotating Matrix for (int i=0; i<N/2; i++){ for (int j=i; j<N-i-1; j++){ int temp = arr[i][j]; arr[i][j] = arr[N-1-j][i]; arr[N-1-j][i] = arr[N-1-i][N-1-j]; arr[N-1-i][N-1-j] = arr[j][N-1-i]; arr[j][N-1-i] = temp; } } // Printing Rotated Matrix for (int i=0; i<N; i++){ for (int j=0; j<N; j++) System.out.print( arr[i][j] + " "); System.out.println(); } } }
Output:
13 9 5 1
14 10 6 2
15 11 7 3
16 12 8 4
Q2. Write a Java Program to convert Decimal Number to Binary Number.
Ans.
import java.util.*; class Main{ public static void main (String[] args){ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); System.out.println("Decimal - " + n); int[] binaryNum = new int[1000]; int i = 0; while (n>0){ binaryNum[i] = n % 2; n = n / 2; i++; } System.out.print("Binary - "); for (int j = i - 1; j >= 0; j--) System.out.print(binaryNum[j]); } }
Input:
28
Output:
Decimal - 28
Binary - 11100
Q3. Write a Java Program to Add Two Numbers without using the Addition Operator.
Ans.
import java.util.*; class Main { public static void main(String[] args){ Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int sum = a; for (int i= 1; i <= b; i++) sum++; System.out.print(sum); } }
Input:
4
5
Output:
9
Q4. Write a program for the Spiral Traversal of a Matrix in Java.
Ans.
class Main{ public static void main(String[] args){ int m = 4; int n = 4; int a[][] = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } }; int i, k = 0, l = 0; while (k<m && l<n) { for (i=l; i<n; ++i) { System.out.print(a[k][i] + " "); } k++; for (i=k; i<m; ++i) { System.out.print(a[i][n-1] + " "); } n--; if (k < m) { for (i=n-1; i>=l; --i) { System.out.print(a[m-1][i] + " "); } m--; } if (l<n) { for (i=m-1; i>=k; --i) { System.out.print(a[i][l] + " "); } l++; } } } }
Output:
1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10
Q5. Write a Java Program to remove duplicates in an array.
Ans.
public class Main { public static int removeduplicates(int a[], int n){ if (n == 0 || n == 1) return n; int[] temp = new int[n]; int j = 0; for (int i = 0; i < n - 1; i++) { if (a[i] != a[i + 1]) { temp[j++] = a[i]; } } temp[j++] = a[n - 1]; for (int i = 0; i < j; i++) { a[i] = temp[i]; } return j; } public static void main(String[] args) { int a[] = { 1, 1, 2, 2, 2 }; int n = a.length; n = removeduplicates(a, n); for (int i = 0; i < n; i++) System.out.print(a[i] + " "); } }
Output:
1 2
Frequently Asked Questions (FAQs)
Here are some Frequently Asked Questions:
Q1: How to prepare for Infosys Java language questions?
A: To prepare for Infosys Java language questions, candidates should focus on mastering the basic concepts of the language and practice coding exercises and sample questions. They can refer to standard textbooks and online resources that cover Java programming in detail. Candidates can also take mock tests and practice previous year’s question papers to get a sense of the type of questions asked and the level of difficulty. Additionally, they can participate in coding challenges and competitions on various online judges to enhance their skills and gain practical experience.
Q2: What is the difficulty level of Infosys Java language questions?
A: The difficulty level of Infosys Java language questions can vary depending on the job position and the level of the candidate’s experience. Typically, Infosys Java language questions for entry-level positions are of moderate difficulty and focus on fundamental concepts, while the questions for experienced positions may be more complex and cover advanced topics. The questions are doable if someone has decent exposure to programming.
Q3: Are Infosys Java language questions only asked in written tests, or are they also asked in interviews?
A: Infosys Java language questions are typically asked in both written tests and interviews. In written tests, candidates may be asked multiple-choice or descriptive questions to test their understanding of the language’s syntax, data types, and programming constructs. In interviews, the questions may be more focused on the candidate’s problem-solving ability, coding skills, and ability to design and implement programs using Java. The interviewer may also ask the candidate to explain their code, provide alternate solutions, and discuss the trade-offs and limitations of different approaches.
Q4. What are other rounds in the Infosys Recruitment Process?
A: The three steps of the Infosys recruiting process are given below:
- Round 1: Online Test
- Round 2: Technical Interview
- Round 3: HR Interview
It should be noted that for some job positions, there may be extra technical rounds in between.