0% found this document useful (0 votes)
1 views

JAVA FAQs

Uploaded by

aarti
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

JAVA FAQs

Uploaded by

aarti
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

How does a hash table work?

Answer: A hash table is a data structure that stores key-value pairs. It uses a hash function to map
keys to indices in an array. When you want to store or retrieve a value, the key is hashed to
determine its index in the array, allowing for fast access. Collisions can occur when multiple keys
hash to the same index. Techniques like chaining or open addressing are used to handle collisions.

What is recursion? Can you provide an example?

Answer: Recursion is a programming technique where a function calls itself to solve smaller
instances of the same problem. Here's an example of calculating the factorial of a number using
recursion in Python:

def factorial(n):

if n == 0:

return 1

else:

return n * factorial(n - 1)

Describe the difference between object-oriented programming and functional programming.

Answer: Object-oriented programming (OOP) is a paradigm that organizes code into reusable
objects, each encapsulating data and behavior. It emphasizes concepts like classes, inheritance, and
polymorphism. Functional programming (FP), on the other hand, treats computation as the
evaluation of mathematical functions. It focuses on immutability, avoiding side effects, and higher-
order functions.

How do you handle exceptions in your code?

Answer: In languages like Python, exceptions are handled using try-except blocks. This allows you to
catch and handle errors gracefully. For example:

try:

result = x / y

except ZeroDivisionError:

print("Error: Division by zero")

except Exception as e:

print("An error occurred:", e)

What is the importance of version control, and can you name some version control systems?

Answer: Version control is crucial for tracking changes to code over time, collaborating with team
members, and reverting to previous states if needed. Some version control systems are Git, SVN
(Subversion), and Mercurial.

Question: Explain the difference between Git and GitHub.


Answer: Git is a distributed version control system used for tracking changes in source code. GitHub,
on the other hand, is a web-based platform that provides hosting for Git repositories. It adds
features like issue tracking, pull requests, and collaboration tools.

Question: How does a linked list differ from an array?

Answer: An array is a data structure that stores elements in contiguous memory locations, allowing
for fast random access but limited flexibility in size. A linked list, on the other hand, consists of nodes
where each node holds data and a reference to the next node. It allows for dynamic sizing and
efficient insertions/deletions but doesn't provide direct random access.

Question: Can you explain the concept of Big O notation?

Answer: Big O notation is used to describe the upper bound or worst-case time complexity of an
algorithm in terms of the input size. It helps in analyzing and comparing the efficiency of algorithms.
For example, O(1) represents constant time, O(n) represents linear time, and O(n^2) represents
quadratic time complexity.

What is a deadlock in multithreading, and how can it be avoided? Answer: Deadlock occurs when
two or more threads are blocked, each waiting for a resource held by another. To avoid deadlocks,
techniques like resource allocation ordering, locking hierarchy, and using timeouts can be employed.

What is a binary tree, and how is it different from a binary search tree (BST)? Answer: A binary tree
is a data structure where each node has at most two children. In a binary search tree, the left child of
a node contains values less than the node's value, and the right child contains values greater than
the node's value.

How does a depth-first search (DFS) algorithm work on a binary tree? Answer: In DFS, the algorithm
starts at the root node and explores as far as possible along a branch before backtracking. There are
two common DFS strategies: in-order, pre-order, and post-order traversal.

Describe the breadth-first search (BFS) algorithm and its typical application. Answer: BFS starts at
the root node and explores all neighbors at the current depth before moving to the next level. It's
often used to find the shortest path in unweighted graphs.

What is the purpose of a heap data structure, and what are its two main variants? Answer: A heap
is a specialized tree-based data structure used to maintain a partially ordered set. The two main
variants are:

 Max Heap: The parent node has a value greater than or equal to its children.

 Min Heap: The parent node has a value less than or equal to its children.
1. Explain how you would reverse a string in Java.

To reverse a string in Java, first of all, you need to declare a string. Then,
you should take out the length of that string and loop through the string’s
characters. Finally, you need to reverse the order of the characters in the
new string.

2. How can you tell if a string is a palindrome?

In a palindrome, the order of characters in a string remains the same


when reversed. To check, reverse the original string and then see if the
reversed string is the same as the original.

How would you implement insertion sort?

You can assume that the array’s first element is sorted. The second
element is stored separately from the first element in the key. Using this
method, you’ll sort the first two elements. Next, take the third element to
compare with the elements to the left of it. Continue the process until you
sort the array.
1. Write the syntax in C to create a node in the singly linked list.

2. Write a Java program that prints out the numbers 1 to 50 but, for multiples of three, print
the word “Fizz” and for multiples of five, print the word “Buzz.” For numbers that are
multiples of both three and five, print the word “FizzBuzz.”
#2.

public class FizzBuzz {

public static void main(String[] args) {

for (int i = 1; i <= 50; i++) {

if (i % 3 == 0 && i % 5 == 0) {

System.out.println("FizzBuzz");

} else if (i % 3 == 0) {

System.out.println("Fizz");

} else if (i % 5 == 0) {

System.out.println("Buzz");

} else {

System.out.println(i);

#1.

struct Node {
int data;
struct Node* next;
};

// Creating a new node


struct Node* newNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}

In this example, we define a struct Node to represent a node in the singly linked
list. It contains two members: an integer data to store the value of the node and a
pointer next to point to the next node in the list. The newNode function is used to
create a new node with the given data value and returns a pointer to the newly
created node. Remember to include the stdlib.h header for using the malloc
function for memory allocation.

You might also like