NPTEL Programming In Java Online Programming Test [April 2022]

NPTEL Programming In Java Online Programming Test [April 2022]:- In This Article, we have helped with the online programming test of Programming In Java.

NPTEL Programming In Java Online Programming Test April 2022 Session 1 [10 AM to 12 PM] SET 1

Q1. Complete the following program that is intended to find and print the minimum value stored in an array ‘arr[]’.

Code:-

int min=arr[0];
for(int i=0;i<arr.length;i++)
{
  if(arr[i]<min)
         min=arr[i];
         }
         System.out.println(min);

Q2. In the following program, an array of type integer (arr[]) is declared. The array can store any integer value. Complete the program  to print the average value of all the odd numbers stored in the array (arr[] ).
Note: Integer type array ‘arr[]’ is already defined and initialized. Use double type variables wherever applicable.

Code:-

double total=0.0,count=0.0;
for(int i=0;i<arr.length;i++)
{
  if(arr[i]%2!=0)
  {
    total=total+arr[i];
    count++;
  }
}
double avg=total/count;
  System.out.print(avg);

Q3. An interface “Square” is defined in the following program. You have to declare a class “A”, which will implement the interface “Square”.
Note that the method ” findSquare(n)” will return the square of the number n.

Code:-

class A implements Square
{
  public int findSquare(int i)
  {
    return i*i;
  }
}

Q4. Complete the following program that is intended to find and print the the first element that is strictly greater than given element in an sorted array ‘arr[]’. If there is no such element then print “-1”.
Note: Integer array ‘arr[]’ is already defined and initialized

Code:-

public static int findnext(int arr[], int target ){
  for(int i=0; i<arr.length; i++){
    if(arr[i] > target){
      return i;
    }
  }
  return -1;
}

Q5. Complete the following program that is intended to find and print the duplicate elements in an array ‘arr[]’.
Note: Integer array ‘arr[]’ is already defined and initialized

Code:-

for(int i = 0; i < arr.length; i++) {  
            for(int j = i + 1; j < arr.length; j++) {  
                if(arr[i] == arr[j])  
                    System.out.println(arr[j]);  
            }  
        }

NPTEL Programming In Java Online Programming Test April 2022 Session 2 [8 PM to 10 PM] Set 2

Q1. Complete the following program that is intended to find and print the third  smallest value stored in an array ‘arr[ ]’.

Note: Integer array ‘arr[]’ is already defined and initialized. Assume array arr[ ] atleast 3 elements.

Code:-

int temp;
for(int i=0;i<(arr.length-1);i++)
{
  if(arr[i]<arr[i+1])
  {
    temp=arr[i];
    arr[i]=arr[i+1];
    arr[i+1]=temp;
  }
}
System.out.print(arr[arr.length-4]);

Q2. Complete the following program that is intended to find and print sum of all unique elements in a sorted array ‘arr[ ]’

Note: Integer array ‘arr[]’ is already defined and initialized

Code:-

int sum=arr[0];
for(int i=0;i<arr.length-1;i++)
{
  if(arr[i]!=arr[i+1])
  {
    sum=sum+arr[i+1];
  }
}
System.out.print(sum);

Q3. Complete the code segment to call the default method in the interface Java and Nptel.

Code:-

Java.super.disp();
Nptel.super.disp();

Q4. Complete the program to find and print the smallest missing element from a sorted array “arr[]”.
NOTE: Non-negative distinct Integer array is already defined and initialized.

Code:-

public static int findMissing(int array[], int start, int end)
    {
        if (start > end)
            return end + 1;
 
        if (start != array[start])
            return start;
 
        int mid = (start + end) / 2;
 
        // Left half has all elements from 0 to mid
        if (array[mid] == mid)
            return findMissing(array, mid+1, end);
 
        return findMissing(array, start, mid);
    }

Q5. Complete the program to count the number of possible triangles from a given sorted array of positive integers(“arr[]”).

Note: The triangle inequality states that the sum of the lengths of any two sides of a triangle must be greater than or equal to the length of the third side.

Code:-

int count = 0; 
 int n = arr.length; 
 
        for (int i = 0; i < n; i++) { 
            for (int j = i + 1; j < n; j++) { 
      
                
                for (int k = j + 1; k < n; k++) 
      
                   
                      if (arr[i] + arr[j] > arr[k]) { 
                        count++; 
                    } 
                     
            } 
        } 
System.out.print(count);

Disclaimer:- We do not claim 100% surety of solutions, these solutions are based on our sole expertise, and by using posting these answers we are simply looking to help students as a reference, so we urge do your assignment on your own.

For More NPTEL Answers:- CLICK HERE

Join Our Telegram:- CLICK HERE

NPTEL Programming In Java Online Programming Test [April 2022]:- In This Article, we have helped with the online programming test of Programming In Java.

Leave a Comment