Java Concepts: Practice Questions and Examples
Java Concepts: Practice Questions
- Primitive data type variables cannot be used to invoke methods.
- Import statements are not needed if one class in a package uses another in the same package.
- Classes declared as
final
cannot be subclassed. - Methods in an abstract class do not have to be abstract.
- Static/private methods cannot be overridden.
Data Type Conversion
- String to int:
int result = Integer.parseInt(string);
Threads
Create and start a thread that checks if an integer is even (divisible by 2):
new Thread(
Pathfinding Algorithms: BFS, DFS, A*, and Uniform Cost
Breadth-First Search (BFS) and Depth-First Search (DFS)
from collections import deque
def bfs(maze, start, end):
rows = len(maze)
cols = len(maze[0])
directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
queue = deque([(start, [start])])
visited = set()
visited.add(start)
node_visited = 0
while queue:
current, path = queue.popleft()
node_visited += 1
if current == end:
return path, node_visited
for r, c in directions:
x = current[0] + r
y = current[
C Language Stack Implementation and Applications
C Code: Checking Parentheses Correctness
#include <stdio.h>
#include <string.h>
#define N 20
typedef struct stack {
int a[N];
int top;
} stack;
void push(stack *s, int x) {
s->top++;
s->a[s->top] = x;
}
int pop(stack *s) {
int x;
x = s->a[s->top];
s->top--;
return x;
}
int isOpenBracket(int x) {
if (x == '{' || x == '[' || x == '(') {
return 1;
} else {
return 0;
}
}
int isCloseBracket(int x) {
if
Read More
IoT Network Architectures: Protocols and Communication
Request-Response Communication in Detail
Request-response communication is a fundamental interaction model in networked systems where a client sends a request to a server, and the server processes the request and sends back a response. This model is widely used in IoT, web services, APIs, and distributed computing.
How Request-Response Communication Works
Client Sends a Request
The client (such as a device, application, or user) sends a request to the server.
The request contains specific information
Read MoreCircuit Switching, Virtual Circuits, and Multiplexing Techniques
A circuit-switched network is made of a set of switches connected by physical links, in which each link is divided into n channels. Figure 8.3 illustrates a trivial circuit-switched network. In circuit switching, resources need to be reserved during the setup phase; the resources remain dedicated for the entire duration of data transfer until the teardown phase.
Virtual-Circuit Networks
A virtual-circuit network is a cross between a circuit-switched network and a datagram network, exhibiting characteristics
Read MoreComputer Networking Fundamentals: Protocols and Hardware
Introduction to Computer Networks
A computer network is a collection of interconnected devices that can communicate with each other and share resources. These devices can include computers, servers, routers, switches, and other hardware. The primary purpose of a computer network is to facilitate communication and resource sharing among the connected devices.
Key Concepts of Computer Networks
1. Types of Networks
- Local Area Network (LAN): A network that covers a small geographical area, like a home,