Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Week4 Day 3 Test and Task

Test:

1. Create the java program to separate the given email address to the following username, domain name
and domain
Input:
ram@gmail.com.in
Output:
Username: ram
Domain name: gmail
Domain: com.in
2. Create the java program to match any IP address within the range 192.168.1.0 to 192.168.1.255.
Input:
192.168.1.12
Output:
Valid
Input 2:
192.168.1.999
Output 2:
Invalid
Task:

1. Create a java program to validate the age is greater than 18. Throw an exception if age is below 18
(print "Access denied"). If age is 18 or older, print "Access granted"

2. You will be given two integers x and y as input, you have to compute x/y. If x and y are not 32 bit
signed integers or if y is zero, exception will occur and you have to report it. Read sample
Input/Output to know what to report in case of exceptions.
Sample Input 0:
10
3
Sample Output 0:
3
Sample Input 1:
10
Hello
Sample Output 1:
java.util.InputMismatchException
Sample Input 2:
10
0
Sample Output 2:
java.lang.ArithmeticException: / by zero
Sample Input 3:
23.323
0
Sample Output 3:
java.util.InputMismatchException

3. Create a class MyCalculator which consists of a single method power(int, int). This method takes two
integers, n and p, as parameters and finds np. If either n or p is negative, then the method must throw
an exception which says "n and p should be non-negative".
 Please read the partially completed code in the editor and complete it. Your class mustn't be
public.
 No need to worry about constraints, there won't be any overflow if your code is correct.
Input Format
Each line of the input contains two integers, n and p.
Constraints
 n
Output Format
Each line of the output contains the result np, if neither of n and p is negative. Otherwise the
output contains "n and p should be non-negative".
Sample Input 0
35
24
00
-1 -2
-1 3
Sample Output 0
243
16
java.lang.Exception: n and p should not be zero.
java.lang.Exception: n or p should not be negative.
java.lang.Exception: n or p should not be negative.
Explanation 0
 In the first two cases, both n and p are postive. So, the power function returns the answer
correctly.
 In the third case, both n and p are zero. So, the exception, "n and p should not be zero.", is
printed.
 In the last two cases, at least one out of n and p is negative. So, the exception, "n or p should not
be negative.", is printed for these two cases.
4. Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by
continuously following the next pointer. Internally, pos is used to denote the index of the node
that tail's next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linke
linked list. Otherwise, return false.
Example 1:

Input: head = [3,2,0,-4],


4], pos = 1
Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).
(0

Example 2:

Input: head = [1,2], pos = 0


Output: true
Explanation: There is a cycle in the linked list, where the tail connects to the 0th node.
Example 3:

Input: head = [1], pos = -1


Output: false
Explanation: There is no cycle in the linked list.
Constraints:
4
 The number of the nodes in the list is in the range [0, 10 ].
5 5
 -10 <= Node.val <= 10
 pos is -1 or a valid index in the linked-list.
5. You are given an array coordinates, coordinates[i] = [x, y], where [x, y] represents the coordinate of a
point. Check if these points make a straight line in the XY plane.
Example 1:

Input: coordinates = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7]]


Output: true
Example 2:

Input: coordinates = [[1,1],[2,2],[3,4],[4,5],[5,6],[7,7]]


Output: false

You might also like