Programming in Java | Week 8

Session: 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


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 NPTEL Programming In Java Week 8 Assignment 8 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?

image 46

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 NPTEL Programming In Java Week 8 Assignment 8 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 NPTEL Programming In Java Week 8 Assignment 8 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: a. Sets the color of the graphics context to be the foreground color of this component.


These are NPTEL Programming In Java Week 8 Assignment 8 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.


Programming Assignment of Programming in Java

Question 1

Write a program which will print a pyramid of “*” ‘s of height “n” and print the number of “*” ‘s in the pyramid.
For example:
Input : 5
Output:  
        * 
      * * * 
    * * * * * 
  * * * * * * * 
* * * * * * * * * 
25

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.println(sum);
    }
}

Question 2

Write a program which will print a pascal  pyramid of  “*” ‘s of height “l” .
For example:
input: 8
output :
       * 
      * * 
     * * * 
    * * * * 
   * * * * * 
  * * * * * * 
 * * * * * * * 
* * * * * * * * 

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 NPTEL Programming In Java Week 8 Assignment 8 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.
For example:
input: 5
output: 
        1 
      1 2 3 
    1 2 3 4 5 
  1 2 3 4 5 6 7 
1 2 3 4 5 6 7 8 9 
95

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.println(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”.
For example:
input : 5
output:
*
 * *
* * *
 * *
  *
input : 6
output:
Invalid line number

Solution:

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

	// Check whether line number is odd
	   if (l%2!=0){
	   ul=(l/2)+1;
	   ll=l-ul;
	//Code for upper half
	for(int i=1;i<=ul; i++){
	//Space management
	for(int s=1;s<=(ul-i); s++){
	    System.out.print(" ");
	}
	// Star management
	for(int j=1;j<=i; j++){
	     System.out.print("* ");
	}
	System.out.println();
	}

	//Code for lower half
	int llc=ll;
	for(int i=1;i<=ll; i++){
	//Space management
	for(int s=llc;s<ll; s++){
	   System.out.print(" ");
	}
	// Star management
	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 NPTEL Programming In Java Week 8 Assignment 8 Answers


Question 5

Write a program to display any digit(n) from 0-9 represented as a “7 segment  display”. 
For example:
input : 5
output :
 _ 
|_ 
 _|
input : 4
output :
|_|
  |

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 = encode(digit);
        char[] bits =
            String.format("%7s", Integer.toBinaryString(code))
                .replace(' ', '0').toCharArray();

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

    private static void lightSegment(boolean on, String onValue, String offValue) {
        System.out.print(on ? onValue : offValue);
        try {
            Thread.sleep(0);
        }
        catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    private static int encode(int digit) {
       return encodings.containsKey(digit) ? encodings.get(digit) : 0x00;
    }
   public static void main(String[] args) throws Exception {
           Scanner inr = new Scanner(System.in);
	      int n = inr.nextInt();
           printDigit(n);
    }
}

These are NPTEL Programming In Java Week 8 Assignment 8 Answers

More Weeks of Programming In Java: Click Here

More Nptel courses: https://progiez.com/nptel/


Session: JULY-DEC 2022

Course Name: Programming in Java NPTEL

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. Button
b. Component
c. Dialog
d. Paint

Answer: d


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

Answer: a


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

Answer: b


These are NPTEL Programming In Java Week 8 Assignment 8 Answers


Q4. Which of the following container does not contain titlebar and menubar 2?
a. window
b. panel
c. container
d. frame

Answer: b


Q5. Which package provides many event classes and Listener interfaces for event handling?
a. java.awt.activeevent
b. java.awt.event
c. java.awt.listener
d. none of these

Answer: b


Q6. What is the name of the method used to get the current font of an graphic in java.awt.graphies?
a. abstract FontMetrics getFontMetrics(Font f)
b. abstract Font getFont()
c. FontMetrics getFontMetrics()
d. None of these

Answer: b


These are NPTEL Programming In Java Week 8 Assignment 8 Answers


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

Answer: b


Q8. Which of the following statement is true 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. Clears this component by filling it with the background color.
d. All of these.

Answer: d


Q9. Which of the following methods can be used to return the current size size of a java.awt.Component object?
a. dimension()
b. setSize()
C. area()
d. size()
e. resize(0)

Answer: d. size()


These are NPTEL Programming In Java Week 8 Assignment 8 Answers


Q10. When we invoke update () for a java.awt.Component object, the A\VT invokes which of the following method?
a. show()
b. draw)
c. paint()
d. repaint()

Answer: c


Programming Assignment Solutions

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:

//Code

These are NPTEL Programming In Java Week 8 Assignment 8 Answers


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

Solution:

//Code

These are NPTEL Programming In Java Week 8 Assignment 8 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:

//Code

These are NPTEL Programming In Java Week 8 Assignment 8 Answers


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”.

Solution:

//Code

These are NPTEL Programming In Java Week 8 Assignment 8 Answers


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

Solution:

//Code

These are NPTEL Programming In Java Week 8 Assignment 8 Answers

More NPTEL Solutions: https://progiez.com/nptel




These are NPTEL Programming In Java Week 8 Assignment 8 Answers