Programming in Java Nptel Week 8 Assignment Answers

Are you looking for Programming in Java Nptel Week 8 Assignment Answers? You’ve come to the right place! Access the latest and most accurate solutions for your Week 8 assignment in the Programming in Java course

Course Link: Click Here


Programming in Java Nptel Week 8 Assignment Answers
Programming in Java Nptel Week 8 Assignment Answers

Programming in Java Nptel Week 8 Assignment Answers (July-Dec 2024)


  1. What does AWT stand for in Java?
    A) Applet Windowing Toolkit
    B) Abstract Window Toolkit
    C) Absolute Windowing Toolkit
    D) Amazing Window Toolkit

Answer: B) Abstract Window Toolkit


  1. In Java, what is the purpose of a Card Layout?
    A) To create a card game interface
    B) To arrange components in a card-like fashion
    C) To manage multiple panels within a single container
    D) To display images of cards

Answer: C) To manage multiple panels within a single container


  1. Which layout manager divides the container into five regions: North, South, East, West, and Center?
    A) Border Layout
    B) Grid Layout
    C) Flow Layout
    D) Card Layout

Answer: A) Border Layout


  1. In Java, what is the primary purpose of a layout manager?
    A) To manage memory allocation
    B) To arrange GUI components within a container
    C) To handle exception handling
    D) To control database connections

Answer: B) To arrange GUI components within a container


  1. What will be the output of the Java code given below?
    A) Compilation error
    B) An empty frame with no button
    C) A frame with a button “Programming in Java – 2024” at coordinates (30, 50)
    D) A frame with a button, but not at the specified coordinates

Answer: C) A frame with a button “Programming in Java – 2024” at coordinates (30, 50)


  1. Which layout manager arranges components in a top-to-bottom flow, adding them to the next available position?
    A) Grid Layout
    B) Flow Layout
    C) Border Layout
    D) Card Layout

Answer: B) Flow Layout


  1. What is the significance of AWT components being heavyweight?
    A) They have higher memory requirements
    B) They are slower in performance
    C) They are dependent on the underlying operating system
    D) They are easier to customize

Answer: C) They are dependent on the underlying operating system


  1. Which AWT concept allows you to handle events such as button clicks or mouse movements?
    A) Event Handling
    B) Function Overloading
    C) Mouse Manager
    D) GUI Processing

Answer: A) Event Handling


  1. Which layout manager organizes components in a grid, with each cell of the grid containing a component?
    A) Flow Layout
    B) Grid Layout
    C) Border Layout
    D) Card Layout

Answer: B) Grid Layout


  1. What is the layout manager used in the Java code given below?
    A) Grid Layout
    B) Border Layout
    C) Flow Layout
    D) Card Layout

Answer: A) Grid Layout


These are NPTEL Programming in Java Nptel Week 8 Assignment Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: https://progiez.com/nptel-assignment-answers


Programming in Java Nptel Week 8 Assignment Answers (JAN-APR 2024)

Course name: Programming In Java

Course Link: Click Here

For answers or latest updates join our telegram channel: Click here to join

These are Programming In Java Week 8 Assignment Answers


Q1. What does AWT stand for in Java?
a. Applet Windowing Toolkit
b. Abstract Window Toolkit
c. Absolute Windowing Toolkit
d. None of the above

Answer: b. Abstract Window Toolkit


Q2. When we invoke repaint() for a java.awt.Component object, the AWT invokes which of the following method ?
a. draw( )
b. show( )
c. update( )
d. none of the mentioned

Answer: c. update( )


For answers or latest updates join our telegram channel: Click here to join

These are Programming In Java Week 8 Assignment Answers


Q3. Which is/are used to create a Frame?
1. By creating the object of Frame class (association)
2. By extending Frame class (inheritance)

a. Only 1
b. Only 2
c. Both
d. None

Answer: c. Both


Q4. Which AWT concept allows you to handle events such as button clicks or mouse movements?
a. Event Handling
b. Component Interaction
c. Process Management
d. GUI Processing

Answer: a. Event Handling


For answers or latest updates join our telegram channel: Click here to join

These are Programming In Java Week 8 Assignment Answers


Q5. Which of the following methods can be used to change the size of a java.awt.Component object?
1. dimension()
2. setSize()
3. area()
4. size()
5. resetSize()

a. 1, 2, 3 & 5
b. 4 & 5
c. 1, 2 & 5
d. only 2

Answer: d. only 2


Q6. Which of the following is TRUE regarding checkbox and radio button?
a. Checkbox is used for single selection item whereas radio button is used for multiple selection.
b. Checkbox is used for multiple selection items whereas radio button is used for single selection.
c. Both are used for multiple as well as single item selection.
d. Checkbox is the same as radio buttons.

Answer: b. Checkbox is used for multiple selection items whereas radio button is used for single selection.


For answers or latest updates join our telegram channel: Click here to join

These are Programming In Java Week 8 Assignment Answers


Q7. How is AWT different from Swing in Java?
a. AWT is for web applications, Swing is for desktop applications
b. AWT uses lightweight components, Swing uses heavyweight components
c. AWT is newer than Swing
d. AWT and Swing are synonyms, used interchangeably

Answer: b. AWT uses lightweight components, Swing uses heavyweight components


Q8. What is the primary purpose of AWT components in GUI programming?
a. To perform mathematical calculations
b. To interact with databases
c. To create graphical user interfaces
d. To handle network communications

Answer: c. To create graphical user interfaces


For answers or latest updates join our telegram channel: Click here to join

These are NPTEL Programming In Java Week 8 Assignment 8 Answers


Q9. What will be the output of the Java code given below?
a. A frame with a button “NPTEL – Programming in Java” at coordinates (30, 50)
b. Compilation error
c. An empty frame with no button
d. A frame with a button, but not at the specified coordinates

Answer: a. A frame with a button “NPTEL – Programming in Java” at coordinates (30, 50)


Q10. What is the layout manager used in the Java code given below?
a. Border Layout
b. Flow Layout
c. Grid Layout
d. Card Layout

Answer: c. Grid Layout


For answers or latest updates join our telegram channel: Click here to join

These are Programming In Java Week 8 Assignment Answers


Programming Assignment

Question 1
A Student class with private fields (name, age) is provided,
Your task is to make the following:
a parameterized constructor to initialize the private fields
the getter/setter methods for each field
Follow the naming convention as given in the main method of the suffix code.

Solution:

class Animal {
    protected String name;
    protected String sound;

    public Animal(String name, String sound) {
        this.name = name;
        this.sound = sound;
    }

    public void makeSound() {
        System.out.println(name + " says: " + sound);
    }
}

class Dog extends Animal {
    public Dog(String name) {
        super(name, "Woof");
    }
}

class Cat extends Animal {
    public Cat(String name) {
        super(name, "Meow");
    }
}

These are Programming In Java Week 8 Assignment Answers


Question 2
A BankAccount class with private field balance is provided,
Your task is to make the following:
a parameterized constructor to initialize the private field
public void deposit(…) // to deposit money
public void withdraw(…) // to withdraw money, should print “Insufficient funds!” if not enough money to withdraw
public double getBalance() // to return the current balance
Follow the naming convention as given in the main method of the suffix code.

Solution:

        double result = 0.0;
        while(num > 0)
          {
               result = result + (double) 1 / num;
               num--;
          }
        System.out.println("Output of Harmonic Series is "+result);

These are Programming In Java Week 8 Assignment Answers


Question 3
An abstract class shape is provided,
Your task is to make the following:
a parameterized constructor to initialize the Circle class
@Override the area function to compute the area of a circle (Use Math.PI for value of pi, not 22/7)
@Override the displayInfo() function to print exactly in the format provided by the test cases.
Follow the naming convention as given in the main method of the suffix code.

Solution:

        for (i = 0; i < row; i++) {
          for (j = 0; j < column; j++) {
            if( i == j )
              sum+= array[i][j];
          }
        }
System.out.print(sum);

These are Programming In Java Week 8 Assignment Answers


Question 4
An interface shape is provided,
Your task is to make the following:
Create a class “Circle” that implements the “Shape” interface and provides its own implementation for calculateArea().
@Override the area function to compute the area of a circle (Use Math.PI for value of pi, not 22/7)
Create another class “Rectangle” that implements the “Shape” interface and provides its own implementation for calculateArea().
@Override the area function to compute the area of a rectangle
Follow the naming convention as given in the main method of the suffix code.

Solution:

static int removeDuplicates(int arr[])
    {
        int n = arr.length;
        if (n == 0 || n == 1)
            return n;
 
        int[] temp = new int[n];
 
        int j = 0;
        for (int i = 0; i < n - 1; i++)
             
            if (arr[i] != arr[i + 1])
                temp[j++] = arr[i];
 
        temp[j++] = arr[n - 1];
 
        for (int i = 0; i < j; i++)
            arr[i] = temp[i];
 
        return j;
    }

These are Programming In Java Week 8 Assignment Answers


Question 5
Two interfaces are provided, namely Flyable and Swimmable,
Your task is to make the following:
Create a class “FlyingFish” that implements both interfaces and provides its own implementation for fly() and swim()
It should have a private variable name, use a constructor to set the value and the functions fly() and swim() to print exactly as given in the test case.
Follow the naming convention as given in the main method of the suffix code.

Solution:

static int determinantOfMatrix(int mat[][], int n)
    {
        int num1, num2, det = 1, index,
                        total = 1; // Initialize result
         int[] temp = new int[n + 1];
         for (int i = 0; i < n; i++){
            index = i; // initialize the index
             while (index < n && mat[index][i] == 0){
                index++;
            }
            if (index == n) // if there is non zero element
            {
                continue;
            }
            if (index != i){
                for (int j = 0; j < n; j++){
                    swap(mat, index, j, i, j);
                }
                det = (int)(det * Math.pow(-1, index - i));
            }
             for (int j = 0; j < n; j++){
                temp[j] = mat[i][j];
            }
            for (int j = i + 1; j < n; j++){
                num1 = temp[i]; // value of diagonal element
                num2 = mat[j][i]; // value of next row element
                 for (int k = 0; k < n; k++) {
                    mat[j][k] = (num1 * mat[j][k])
                                - (num2 * temp[k]);
                }
                total = total * num1; // Det(kA)=kDet(A);
            }
        }
        for (int i = 0; i < n; i++) {
            det = det * mat[i][i];
        }
        return (det / total); // Det(kA)/k=Det(A);
    }
    static int[][] swap(int[][] arr, int i1, int j1, int i2,
                        int j2){
        int temp = arr[i1][j1];
        arr[i1][j1] = arr[i2][j2];
        arr[i2][j2] = temp;
        return arr;
}

For answers or latest updates join our telegram channel: Click here to join

These are Programming In Java Week 8 Assignment Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: https://progiez.com/nptel-assignment-answers


Programming in Java Nptel Week 8 Assignment Answers (July-Dec 2023)

Course Name: Programming In Java

Course Link: Click Here

These are NPTEL Programming In Java Week 8 Assignment 8 Answers

Question 1
Write a program which will print a pyramid of “*” ‘s of height “n” and print the number of “*” ‘s in the pyramid.

Solution:

import java.util.*;
public class Pattern1 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	    int n = inr.nextInt();
        int k = 0,sum=0;
        for(int i = 1; i <= n; ++i, k = 0)
        {
            for(int space = 1; space <= n - i; ++space)
            {
                System.out.print("  ");
            }
            while(k != 2 * i - 1)
            {
                System.out.print("* ");
                sum=sum+1;
                ++k;
            }
            System.out.println();
        }
        System.out.print(sum);
    }
}

Question 2
Write a program which will print a pascal pyramid of “*” ‘s of height “l” .

Solution:

import java.util.*;
public class Pattern2 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	    int l = inr.nextInt();
        int i,j;
        int space=l-1;
        for(i=0;i< l;i++)
        {
            for(j=0;j< space;j++)
            {
                System.out.print(" ");
            }
            for(j=0;j<=i;j++)
            {
                System.out.print("* ");
            }
            System.out.print("\n");
            space--;
         }
     }
}

These are Programming In Java Week 8 Assignment Answers


Question 3
Write a program which will print a pyramid of “numbers” ‘s of height “n” and print the sum of all number’s in the pyramid.

Solution:

import java.util.*;
public class Pattern3 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	    int n = inr.nextInt();
        int k = 1,sum=0;
        for(int i = 1; i <= n; ++i, k = 1)
        {
            for(int space = 1; space <= n-i; ++space)
            {
                System.out.print("  ");
            }
            while(k <= 2 * i - 1)
            {
                System.out.print(k+" ");
                sum=sum+k;
                ++k;
            }
            System.out.println();
        }
        System.out.print(sum);
    }
}

Question 4
Write a program to print symmetric Pascal’s triangle of “*” ‘s of height “l” of odd length . If input “l” is even then your program will print “Invalid line number”.

import java.util.*;
public class Pattern4 {
    public static void main(String[] args) {
        Scanner inr = new Scanner(System.in);
	  	int l = inr.nextInt();
        int ul=0; // Upper Line
        int ll=0; // Lower Line

        if (l%2!=0)
        {
           ul=(l/2)+1;
           ll=l-ul;
           for(int i=1;i<=ul; i++)
           {
          		for(int s=1;s<=(ul-i); s++)
                {
              		System.out.print(" ");
          		}
          		for(int j=1;j<=i; j++)
                {
               		System.out.print("* ");
          		}
          		System.out.println();
          }

          int llc=ll;
          for(int i=1;i<=ll; i++)
          {
          		for(int s=llc;s<ll; s++)
                {
             		System.out.print(" ");
          		}
          		for(int j=1;j-1<=ll-i; j++)
                {
             		System.out.print(" *");
           		}
           		llc--;
          		System.out.println();
          }
       }
       else
       {
       		System.out.print("Invalid line number");
       }
    }
}

These are Programming In Java Week 8 Assignment Answers


Question 5
Write a program to display any digit(n) from 0-9 represented as a “7 segment display”.

Solution:

import java.util.*;
public class Pattern5{
   private static final Map<Integer,Integer> encodings=new HashMap<Integer,Integer>();

    static{
        encodings.put(0, 0x7E);
        encodings.put(1, 0x30);
        encodings.put(2, 0x6D);
        encodings.put(3, 0x79);
        encodings.put(4, 0x33);
        encodings.put(5, 0x5B);
        encodings.put(6, 0x5F);
        encodings.put(7, 0x70);
        encodings.put(8, 0x7F);
        encodings.put(9, 0x7B);
    }

    public static void printDigit(int digit)
    {
        int code = encodeDigit(digit);
        char[] bits = String.format("%7s",Integer.toBinaryString(code))
          .replace(' ','0').toCharArray();

        lightSegmentDisp(bits[0] == '1', " _ \n", "   \n");
        lightSegmentDisp(bits[5] == '1', "|", " ");
        lightSegmentDisp(bits[6] == '1', "_", " ");
        lightSegmentDisp(bits[1] == '1', "|\n", " \n");
        lightSegmentDisp(bits[4] == '1', "|", " ");
        lightSegmentDisp(bits[3] == '1', "_", " ");
        lightSegmentDisp(bits[2] == '1', "|\n", " \n");
    }

    private static void lightSegmentDisp(boolean On, String onVal, String offVal)
    {
        System.out.print(On ? onVal : offVal);
        try {
            Thread.sleep(0);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private static int encodeDigit(int d) {
        return encodings.containsKey(d) ? encodings.get(d) : 0x00;
    }
   public static void main(String[] args) throws Exception {
           Scanner inr = new Scanner(System.in);
	   	   int n = inr.nextInt();
           printDigit(n);
    }
}

These are Programming In Java Week 8 Assignment Answers

More Weeks of Programming In Java: Click here

More Nptel Courses: Click here


Programming in Java Nptel Week 8 Assignment Answers (JAN-APR 2023)

Course Name: Programming in Java

Course Link: Click Here

These are NPTEL Programming In Java Week 8 Assignment 8 Answers


Q1. Which of the following is NOT a class of java.awt package?
a. Color
b. Composite
c. Container
d. Cursor

Answer: b. Composite


These are NPTEL Programming In Java Week 8 Assignment 8 Answers


Q2. Which of the following is/are NOT an exception of java.awt package?
a. HeadlessException
b. AWTException
c. FontFormatException
d. IllegalStateException

Answer: d. IllegalStateException


These are Programming In Java Week 8 Assignment Answers


Q3. Which of the following method is/are of class button in java.awt package?
a. getLabel()
b. setLabel(String label)
c. getCurrent()
d. getltem(int index)

Answer: a, b


Q4. Which of the following is TRUE about the following GUI?

a. There is a Frame and two TextFields.
b. There is a Frame with two Labels and two non-editable TextFields.
c. There are two Labels.
d. There is a Frame with two Labels and two TextFields.

Answer: d. There is a Frame with two Labels and two TextFields.


These are Programming In Java Week 8 Assignment Answers


Q5. Which of the following is TRUE regarding check box and radio button?
a. Check box is used for single selection item whereas radio button is used for multiple selection.
b. Check box is used for multiple selection items whereas radio button is used for single selection.
c. Both are used for multiple as well as single item selection.
d. Checkbox is always preferred than radio buttons.

Answer: b. Check box is used for multiple selection items whereas radio button is used for single selection.


Q6. Which of the following is TRUE about check box in Java?
a. A check box can be in either an “on” (true) or “off” (false) state.
b. Clicking on a check box changes its state from “on” to “off.” or from “off” to “on.”
c. A check box can be in an “on” (true) and in “off” (false) state simultaneously.
d. Check boxes cannot be grouped together.

Answer: a, b


These are Programming In Java Week 8 Assignment Answers


Q7. Which of the following method is used to remove all items from scrolling list in java.awt.list?
a. hide()
b. remove()
c. clear()
d. close()

Answer: c. clear()


Q8. Which of the following statement is FALSE about the update() in java.awt package?
a. Sets the color of the graphics context to be the foreground color of this component.
b. Calls this component’s paint method to completely redraw this component.
c. Updates the component by checking an online repository.
d. Clears this component by filling it with the background color.

Answer: c. Updates the component by checking an online repository.


These are Programming In Java Week 8 Assignment Answers


Q9. Which of the following is the latest graphics and media package for Java?
a. Applet
b. AWT
c. Swing
d. JavaFX

Answer: d. JavaFX


Q10. If setText(String text) is a method of Label class, then why it is called non editable?
a. Because user-input is unavailable during runtime.
b. A Label is just a TextField with non-editable property turned on.
c. Labels are editable by default.
d. setText(String text) is not a method of Label class.

Answer: a. Because user-input is unavailable during runtime.