InfyTQ Previous Year Java Questions
Q. Consider the given code below :
import java.time.LocalDate;
class employee {
private String name;
employee(String name) {
this.Name = name;
}
boolean validateDetails(){
for(int index=0; index<this.Name.length();index++)
{
if((this.Name.charAt(index) >= 'A' && this.Name.charAt(index) <= 'Z')
||(this.Name.charAt(index) >= 'a' && this.Name.charAt(index) <= 'z')){}
else{
return false;
}
return true;
}
}
Find the programming violations in this code :
i. Unused import statements
ii. Empty if
iii. class name and variable naming conventions
iv. Usage of default access modifiers
Choose the correct option :
a. i, ii, iii
b. i, iv
c. iii only
d. i, iii
Ans. c
Q. Consider the below code that results in a compilation error :
abstract class Parent {
Parent(){
System.out.println("Parent Class");
}
abstract void display();
}
public class Child extends Parent {
Child() {
System.out.println("Child Class");
}
public static void main(String[] args)
{
Parent parent = new Child();
}
}
Identify TWO ways from the given options which can be used to rectify the compilation error.
a. Make the Child class abstract and remove the child object created in main method.
b. Make the reference and object of same type in the main method.
c. Inherit method display() in Child
d. Remove the constructor in Parent class
Ans. a, d
Q. An Employee Management System application is used to maintain information about employees in an organization. In the application, employee details are stored in ascending order of the employee ids. Which algorithmic design technique would best fit if an employee needs to be searched based on the ids.
a. Greedy Approach
b. Brute Force
c. Divide & Conquer
d. Dynamic Programming
Ans. c
Q. Consider the below code that results in a compilation error :
abstract class Parent {
Parent(){
System.out.println("Parent Class");
}
abstract void display();
}
public class Child extends Parent {
Child() {
System.out.println("Child Class");
}
public static void main(String[] args)
{
Parent parent = new Child();
}
}
Identify TWO ways from the given options which can be used to rectify the compilation error.
a. Make the Child class abstract and remove the child object created in main method.
b. Make the reference and object of same type in the main method.
c. Inherit method display() in Child
d. Remove the constructor in Parent class
Ans. a, d
