Company-Specific Courses
Free Resources
Webinars
About Us

Edit
Reply




Edit

InfyTQ Questions | InfyTQ Final Round Java MCQs

Published on 16 Apr 2021

This article covers the InfyTQ Final Round MCQs in Java. Post the initial Qualifying Round, the Final Round in InfyTQ is what determines whether candidates get the certificate of Infosys Certified Software Programmer and also potentially land a job offer in Infosys. What's covered in this article is solutions in Java to the InfyTQ Questions (MCQs), which are part of the official sample paper.


InfyTQ Questions and Answers | Java MCQs


Q1. What will be the output of the below code?

class ListExample
{
    public static void main(String[] args
    {
      List<String> list = new ArrayList<>(); 
      list.add("I"); 
      list.add("Love"); 
      list.add("Java"); 
      list.add("Language"); 
      Iterator<Object> iter = list.iterator(); 
      while (iter.hasNext())
         System.out.print (iter.next().toString() + " "); 
      System.out.println();
     }
}

Assumption: All classes, interfaces, and necessary methods are available.


A. I Love Java Language

B. Error: Incompatible types: String cannot be converted to Object

C. Error: iterator cannot be created for Object

D. Error: to string() cannot be applied on a string object


Answer: Option B

Explanation: We can not create an iterator of type Object for a list of type String. This will leads to an error Incompatible types: String cannot be converted to Object.


Q2. What is the output of the following code snippet?

class Bill{
    int itemPrice; 
    public Bill (int itemPrice) { this.itemPrice = itemPrice;}
    void display() {
        int itemPrice = 20; 
       System.out.println(itemPrice);
    }
}
class Demo { 
     public static void main(String[] args) {
        Bill billobj = new Bill (10); 
        System.out.println (billobj.itemPrice); 
         billobj.display();
     }
}


A. 10

0

B. 10

20

C. 10

10

D. Error in the class as there is no default constructor defined


Answer: Option B

Explanation: When we are creating the object(billobj) for the class Bill the constructor will initialize the billobj.itemPrice as 10 and the same is printed. But when we are calling the function display() the variable itemPrice will be created and it will be treated as a local variable. Hence the output will be 20.


Q3. Consider the Binary Search code given below:

public static int search(int arrayofElements[], int low, int high, int elementToBeSearched) 
{
   if (low <= high) 
   {
     int mid = (low + high) / 2; 
     if (arrayofElements[mid] == elementToBeSearched)
        return mid; 
      if (arrayofElements[mid] < elementToBeSearched)
        return search(arrayofElements, mid + l, high, elementToBeSearched); 
     return search(arrayOfElements, low, mid - l, elementToBesearched);
   }
   return -1;
}


Consider the arrayOfElements having 6 elements with low as 0 and high as 5. The elements of the array are as follows.


5 6 9 12 15 29


Find the number of iterations when using binary search if the elementToBeSearched is 6?


A. 1

B. 2

C. 3

D. 4


Answer: Option C

Explanation:

In the first Iteration, low is 0, and high is 5. The mid-value will be 2. arrayOfElements[2] is 9.

Since 9 > 6 the statement search(arrayOfElements, low, mid - l, elementToBesearched) will get executed.

In the second iteration, low is 0, and high is 1. The mid-value will be 0. arrayOfElements[0] is 5.

Since 5 < 6 the statement search(arrayofElements, mid + l, high, elementToBeSearched) will get executed.

In the third iteration, low is 1, and high is 1. The mid-value will be 1. arrayOfElements[1] is 6.

Since 6 == 6 the mid-value will be returned and the program execution stops.


So, It takes 3 iterations to find the index position of 6.


Q4. Consider the code given below:

class Student {
   private int studentId; 
   private String studentName; 
   Student (int studentId, String studentName) {
       this.studentId = studentId; 
       this.studentName = studentName;
   }
}
class College {
    private Student student; 
    private int basicFees; 
    College (Student student, int basicFees) {
       this.student = student; 
       this.basicFees = basicFees;
    }
}

Identify the relationship between Student and College classes.

A. Aggregation

B. Association

C. Inheritance

D. The two classes are not related


Answer: Option A

Explanation: The class College definitely required object Student so that it works perfectly. Class Student is independent of class College. Class College is dependent of Student this type of relationship is defined as Aggregation.


Q5. Consider the code given below.

Identify the code that needs to be filled in Line 1, 2, and 3 respectively such that:

• The student id is auto-generated starting from 501 in steps of 1 

• The method 'getNoOfStudent' returns the total number of students enrolled at any given point.


class Student{
   private int studentId; 
   private String studentName; 
   private int yearofEnrollment; 
   public static int counter; 
   static {
       //Line 1
   }
   public Student (String name, int yearOfEnrollment) {
      this.studentName=name; 
      this.yearOfEnrollment=yearofEnrollment;
                // Line 2
   }
   public static int getNoOfStudent () {
                // Line 3
    }
}


A. Line 1: Student.counter=501; 

Line 2: this.studentid=Student.counter++; 

Line 3: return (Student.counter-500);


B. Line 1: Student.counter=501; 

Line 2: this.studentid=++Student.counter; 

Line 3: return (Student.counter-501);


C. Line 1: Student.counter=500; 

Line 2: this.studentId=Student.counter++; 

Line 3: return (Student.counter-500);


D. Line 1: Student.counter=500; 

Line 2: this.studentid=++Student.counter; 

Line 3: return (Student.counter-500);


Answer: Option D

Explanation: To get the desired output we need to execute the statements in the following order

Line 1: Student.counter=500; 

Line 2: this.studentid=++Student.counter;

Line 3: return (Student.counter-500);


Q6. Consider the code snippet given below:

class Customer {
   public int custId; 
   public String custName;
}
public class Tester { 
   public static void main(String args[]) {
      Customer obj = new Customer(); 
      Customer objone = new Customer(); 
      Customer objTwo; 
      Customer objThree = obj;
   }
}


How many object and reference variables of class Customer will be created?


A. 3 objects and 1 reference variable

B. 2 objects and 4 reference variables

C. 4 objects and 4 reference variables

D. 2 objects and 3 reference variables


Answer: Option D

Explanation:

Customer obj = new Customer(); Here an object is created and its reference is given to obj.

Customer objone = new Customer(); Here also an object is created and its reference is given to objone

Customer objTwo; Here a variable is created. It will neither be considered object nor reference. It will be just treated as a local variable.

Customer objThree = obj; The reference of obj is stored in objThree.


Objects: obj, objone.

Reference variable: obj. objone, objThree


Q7. Consider the code given below:

class ClassA{ 
   void firstMethod() {
       System.out.println("Johnny Johnny..., ");
   }
   void secondMethod() {
      System.out.println("Yes Papa. ");
   }
   void thirdMethod() {
      System.out.println("Eating Sugar..., ");
   }
}
class ClassB extends ClassA{ 
   void secondMethod(){
   	super.firstMethod(); 
   	super. secondMethod();   	
   	super.thirdMethod(); 
   	System.out.println("No Papa. ");
   }
   void thirdMethod() {
    System.out.println("Telling Lies..., ");
   }
}
class ClassC extends ClassB{ 
    void firstMethod() {
       System.out.println("Open your mouth..., Ha. Ha. Ha.");}
    void secondMethod() {
       System.out.println("No Papa. ");}
    void thirdMethod() {
       super.secondMethod(); 
       super. thirdMethod(); 
       this. secondMethod();
    }	
	public static void main(String[] args) {
       ClassA objA= new ClassA(); 
       ClassB objB=new ClassB(); 
       ClassC objC=new ClassC(); 
       //Line 1
   }
}


Which among the below options if written at //Line 1, prints the rhyme correctly? 

Choose two CORRECT options. 

The expected output for your reference:


Johnny Johnny. . . , 

Yes Papa. 

Eating Sugar. . ., 

No Papa. 

Telling Lies . . ., 

No Papa. 

Open your mouth. . ., Ha. Ha. Ha.


A. objA.firstMethod(); 

 objA.secondMethod(); 

 objA.thirdMethod(); 

 objC.firstMethod();

B. objC.thirdMethod(); 

 objC.firstMethod();

C. objB.secondMethod(); 

 objB.thirdMethod(); 

 objC.secondMethod(); 

 objC.firstMethod();

D. objA.firstMethod();  

 objB.secondMethod(); 

objC.thirdMethod(); 

objC.firstMethod();


Answer: Option C

Explanation: To get the output in the expected order we need to call the functions in this order.

objB.secondMethod(); 

objB.thirdMethod(); 

objC.secondMethod(); 

objC.firstMethod();


Q8. Consider an input queue inQueue of Strings with the following elements:


inQueue(Front-> Rear): "Crib", "Bat", "Crab", "Carl", "Cat", "Row"


What will be the output of the below function if the above inQueue and the String "Par" are passed as input parameters?


public static ArrayDeque<String> compareStrings(Queue inQueue, String inString) {
   ArrayDeque<String> outStack=new ArrayDeque<String>(6); 
   String tempString="Empty"; 
   while (!inQueue.isEmpty()) { 
      if (! (inQueue.poll().length()==inString.length())) {
          outStack.push(inQueue. poll());
      }
      else{
      	  tempString=inQueue. poll(); 
         outStack.pop();
      }
   }
     outStack.push(tempString); 
     return outStack;
}


Assumptions:

• Queue class, with the necessary methods, is available 

• ArrayDeque class, with the necessary methods, is available


A. outStack(Top->Bottom): [Empty]

B. outStack(Top->Bottom): [Empty, Bat]

C. outStack(Top->Bottom): [Row, Bat]

D. outStack(Top->Bottom): [Row]


Answer: Option C

Explanation: Each time when you call the function poll() it will retrieve and remove the first element in Queue. After all the statements got executed the elements stored in the ArrayDeque outStack are Row and Bat.


Q9. An Employee Management System application is used to maintain information about employees in an organization. In the application, employee details are stored in the ascending order of the employee Ids. Which algorithmic design technique would best fit if an employee needs to be searched based on the employee Id.


A. Greedy Approach 

B. Brute Force

C. Divide and Conquer

D. Dynamic Programming


Answer: Option C

Explanation: Since the elements are in sorted order we can use Binary Searching which follows the Divide and Conquer algorithm.


Q10. What is the output of the following code?

class Base { 
   private int fun() {
      return 0;
   }
    public int run() {
      return 3;
   }
}
class Derived extends Base { 
   private int fun() {
      return 1;
   }
   public int run() {
      return fun();
   }
}
class Derivedl extends Derived { 
    public int fun() {
       return 2;
    }
}
class Tester { 
    public static void main(String[] args) {
        Base baseRef = new Derivedl(); 
        System.out.println (baseRef.run());
    }
}


A. 1

B. 2

C. 0

D. 3


Answer: Option A

Explanation: baseRef.run() is called and in inheritance always the children's class function will be called. In this program, the run() function in the Derived class is called. Inside the function, another function named fun() will be called since the fun() is defined in the same class the value 1 is returned as output.


 If you want to check out the video explanation of these questions, please watch the video below.




Suggested Articles:


InfyTQ Questions and Answers | Python Qualifying Round

InfyTQ 2022 batch | Infosys InfyTQ Registration & Process

HackWithInfy 2021 | Infosys Coding Contest for 2022 batch

InfyTQ Questions and Answers | InfyTQ Aptitude for Qualifying Round

InfyTQ Questions and Answers | Java Qualifying Round

InfyTQ Final Round Python Coding Questions

InfyTQ Questions | InfyTQ Final Round DBMS Questions

InfyTQ Questions and Answers | Java Qualifying Round


If you have any feedback about this article and want to improve this, please write to enquiry@faceprep.in
Explore 'infosys'
Articles