Programming in Java NPTEL Week 7 Assignment

                     

Course Name: Programming in Java NPTEL

Link of Course: https://onlinecourses.nptel.ac.in/noc22_cs47/preview

These are answers for Programming In Java NPTEL Week 7 Assignment


1) Which of these is method for testing whether the specified element is a file or a directory?
a. IsFile()
b. isFile()
c. Isfile()
d. isfile()

Answer: b. isFile()


2) Which of the following is/are NOT Standard Stream(s)?
a. System.in
b. System.out
c. System.err
d. System.console

Answer: d. System.console


3) What will be the output of the Java code?
a. NPTEL
b. NPTEL/JULY/2022
c. /NPTEL/JULY/2022
d. 2022

Answer: d. 2022


These are answers for Programming In Java NPTEL Week 7 Assignment


4) What will be the output if the above program is executed?
a. It will give compile-time error
b. It will give run-time error
c. j
d. 106

Answer: d. 106


5) Which method is used to write a byte to the current output stream?
a. public void write(int b)throws IOException
b. public void write(byte[] b)throws IOException
c. public void flush()throws IOException
d. public void close() throws IOException

Answer: b. public void write(byte[] b)throws IOException


6) Which method of Random AccessFile class reads a line from the file and returns String ?
a. WriteInt()
b. readLine()
c. readInt()
d. WriteDouble()

Answer: b. readLine()


These are answers for Programming In Java NPTEL Week 7 Assignment


7) Which of these class is not a member class of java.io package?
a. File
b. PrintStream
c. StringReader
d. Stream

Answer: d. Stream


8) Which of the following is/are interface(s) of java.io package?
a. FileReader
b. File Writer
c. DataOutput
d. DataInput

Answer: c, d


9) In which Java APIs the classes for handling all IO-streams are defined?
a. java.lang
b. java.util
c. java.io
d. java.awt

Answer: c. java.io


These are answers for Programming In Java NPTEL Week 7 Assignment


10) Consider the following program.
If the program is executed, then what will be the output from the execution?

a. length : 6
b. length : 5
c. length : 0
d. length : 1

Answer: b. length : 5


Programming Assignment Solutions

Question 1
Complete the following code fragment to read three integer values from the keyboard and find the sum of the values. Declare a variable “sum” of type int and store the result in it.

Solution:

import java.util.*;
public class Question1{
        public static void main (String[] args){

          int i,n=0,sum=0;
          Scanner in = new Scanner(System.in);
          for(i=0;i<3;i++)
          {
              n = in.nextInt();
              sum =sum+n;
          }
          System.out.println(sum);
    }
}

These are answers for Programming In Java NPTEL Week 7 Assignment


Question 2
Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “Please enter valid data” .If there is no such exception, it will print the “square of the number”.

Solution:

import java.io.*;
public class Question2{
public static void main(String args[]){

      try{
           InputStreamReader r=new InputStreamReader(System.in);
           BufferedReader br=new BufferedReader(r);
           String number=br.readLine();
           int x = Integer.parseInt(number);
           System.out.print(x*x);
         }
      catch(Exception e){
           System.out.print("Please enter valid data");
         }
    }
}

These are answers for Programming In Java NPTEL Week 7 Assignment


Question 3
A byte char array is initialized. You have to enter an index value”n”. According to index your program will print the byte and its corresponding char value.
Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, it will print the required output.

Solution:

import java.util.*;
public class Question3 {
    public static void main(String[] args) {
       try{
        	  byte barr[]={'N','P','T','E','L','-','J','A','V','A','J','A','N','-','N','O','C','C','S','E'};
              Scanner inr = new Scanner(System.in);
        	  int n = inr.nextInt();
        	 String s2 = new String(barr,n,1);
              System.out.println(barr[n]);
        	  System.out.print(s2);
        }
       catch (Exception e){
    	    System.out.println("exception occur");
        }
    }
}

These are answers for Programming In Java NPTEL Week 7 Assignment


Question 4
The following program reads a string from the keyboard and is stored in the String variable “s1”. You have to complete the program so that it should should print the number of vowels in s1 . If your input data doesn’t have any vowel it will print “0”.

Solution:

import java.io.*;
import java.util.*;

public class Question4{
    public static void main(String[] args) {
	    int c=0;
         try{
                InputStreamReader r=new InputStreamReader(System.in);
                BufferedReader br=new BufferedReader(r);
                String s1 = br.readLine();
                for(int i=0;i< s1.length();i++)
                {
                    char s2=s1.charAt(i);
                    if(s2=='e' || s2=='E'|| s2=='a' || s2=='A' || s2=='i' || s2=='I' || s2=='o' || s2=='O' || s2=='u' || s2=='U')
                   	{
                     	c=c+1;
                    }
             	}
             	System.out.println(c);
	        }
         catch(Exception e){
    		    System.out.println(e);
    	    }
    }
}

These are answers for Programming In Java NPTEL Week 7 Assignment


Question 5
A string “s1” is already initialized. You have to read the index “n”  from the keyboard. Complete the code segment to catch the exception in the following, if any. On the occurrence of such an exception, your program should print “exception occur” .If there is no such exception, your program should replace the char “a” at the index value “n” of the “s1” ,then it will print the modified string.

Solution:

import java.util.*;
public class Question5 {
    public static void main(String[] args) {
       try{
    	    String s1="NPTELJAVA";
            Scanner inr = new Scanner(System.in);
    	    int n = inr.nextInt();
            char c='a';
            byte[] barr=s1.getBytes();

            byte b1 = (byte) c;
            barr[n]=b1;
    		System.out.print(new String(barr));
	    }
	    catch (Exception e){
            System.out.println("exception occur");
	    }
    }
}

These are answers for Programming In Java NPTEL Week 7 Assignment

All Weeks of Programming In Java: https://progies.in/answers/nptel/programming-in-java

More NPTEL Solutions: https://progies.in/answers/nptel


* The material and content uploaded on this website are for general information and reference purposes only. Please do it by your own first.



These are answers for Programming In Java NPTEL Week 7 Assignment