Review Questions - Kasun
Review Questions - Kasun
1.) class B{ public static void main(String args[]){ boolean b = false; int i = 1; do{ i++; b = !b; }while(b); System.out.println(i); } } Choose the Output? 1) 2 2) 3 3) 1 4) Compile Error 2.) BorderLayout manager positions to place components are; 1) North, South, East, West, Center 2) Up, Down, Left, Right, Middle 3) North, Middle, South, West, East 4) Up, Down, Center, Left, Right 3.) What is the correct way to define a final variable? 1) final a = 5; 2) Final a; a = 5; 3) final int a = 5; 4) final int a; int = 5; 4.) Select the encapsulated variable; 1) public int b; 2) final public int b = 1; 3) private int b; 4) abstract int b; 5.) What is NOT an advantage of encapsulation? 1) Data hiding 2) Immutability 3) Data validation 4) More codes
Short Answer Questions 1.) What are three requirement of the basic model for a GUI program? 2.) Write the code to create a BorderLayout manager?
What is the Output of the Program? 1.) class A{ public static void main(String args[]){ final int a = 3; final ina b; b = 2; int x = 1; switch(x){ case a: System.out.println(A); case b: System.out.println(B); } } }
2.) class A{ public static void main(String args[]){ int x = 3; switch(x){ case 1: System.out.println(A); case 2: System.out.println(B); break; case 3: System.out.println(C); case 4: System.out.println(D); break; default : System.out.println(E); } } }
Write a program Write the code to display the following; Hint: use GridLayout and FlowLayout managers
textBox
buttons
Answers:
MCQ: Q1) 2 Q2) 1 Q3) 3 Q4) 3 Q5) 4 Short Answers: Q1) 1. Declaring application as event listener (action listener) (e.g. implements ActionListener) 2. Register the reactive components (e.g. tFahr.addActionListener (this)) 3. Define the action to be performed (methods) (e.g. action Performed (ActionEvent e)) Q2) BorderLayout layout = new BorderLayout(0, 0); setLayout(layout);
import java.awt.*; class Calc{ public static void main(String args[]){ myCalc m = new myCalc(); System.out.println(); } } class myCalc extends Frame{ myCalc(){ setTitle("myClac"); setSize(300,100); Button b1 = new Button(" 1 "); Button b2 = new Button(" 2 "); Button b3 = new Button("Add"); TextField t = new TextField(20); add(t,BorderLayout.NORTH); Panel p = new Panel(); add(p,BorderLayout.CENTER); GridLayout g = new GridLayout(3,3,2,2); FlowLayout ff = new FlowLayout(); p.setLayout(ff); p.add(b1); p.add(b2); p.add(b3); setVisible(true); } }