IP Course - Java Tour Part 1 by Sumita Arora

13
Java GUI Programming Revision Tour-I Type A: Very Short/Short Answer Questions 1 What is RAD? Ans: Rapid Application Development (RAD) describes a method of developing software through the use of pre- programming tools or wizards. The pre-programmed tools or controls are simply dropped on a screen to visually design the interface of application. 2 What are RAD tools? Ans: RAD tools are the tool that enables one to create applications in shorter time as compared to conventional language. 3 What is event? What is message? How is it related to an event? Ans: An event refers to the occurrence of an activity. A message is the information/request sent to the application. Each time an event occurs, it causes a message to be sent to the operating system. 4 What are properties? What is property window? Ans: Properties are the attributes of graphical controls. Properties window displays the editable settings for the currently selected component. 5 What are containers? Give examples. Ans: A container is a control that can hold other controls within it. For example, JPanel, JFrame, JApplet and JDialog are container classes. 6 What are child controls? Ans: The container control is the one that can contain/hold other controls in it. The contained controls are known as child controls. 7 What happens to container control and its child controls if you delete (i) The container control, (ii) A child control (iii) All child controls? Ans: (i) If container control is deleted then with container control child control will be also deleted (ii) If a child control is deleted then the container control will be remain and a child control will be deleted. (iii) If all child controls is deleted then the container control will be remain and all the child control will be deleted. 8 Name the character set supported by java. Ans: Java uses the Unicode character set. 9 What are keywords? Can keywords be used as identifiers? Ans: Keywords are the words that convey a special meaning to the language compiler. These are reserved for special purpose. Keywords cannot be used as identifiers. 10 What is an identifier? What is the identifier forming rule of java? Ans: Identifiers are user define names for different parts of program. Identifier forming rules of java state the following: 1. Identifiers can have alphabets, digits, and underscore and dollar sign characters. 2. They must not be a keyword or Boolean literal or null literal. 3. They must not begin with a digit. 4. They can be of any length. 5. Java is case sensitive i.e., upper-case letters and lower case letters are treated differently. 11 Is java case sensitive? What is meant by the term ‘case sensitive’? Ans: Java is case sensitive. Case sensitive meant upper-case letters and lower case letters are treated differently. 12 Which of the following are valid identifiers and why/why not? http://cbsecsnip.in Page 1 of 13

description

IP Course - Java Tour Part 1 by Sumita Arora

Transcript of IP Course - Java Tour Part 1 by Sumita Arora

Page 1: IP Course - Java Tour Part 1 by Sumita Arora

Java GUI Programming Revision Tour-I

Type A: Very Short/Short Answer Questions 1 What is RAD? Ans: Rapid Application Development (RAD) describes a method of developing software through the use of pre-

programming tools or wizards. The pre-programmed tools or controls are simply dropped on a screen to visually design the interface of application.

2 What are RAD tools? Ans: RAD tools are the tool that enables one to create applications in shorter time as compared to conventional language. 3 What is event? What is message? How is it related to an event? Ans: An event refers to the occurrence of an activity.

A message is the information/request sent to the application. Each time an event occurs, it causes a message to be sent to the operating system.

4 What are properties? What is property window? Ans: Properties are the attributes of graphical controls.

Properties window displays the editable settings for the currently selected component. 5 What are containers? Give examples. Ans: A container is a control that can hold other controls within it. For example, JPanel, JFrame, JApplet and JDialog are

container classes. 6 What are child controls? Ans: The container control is the one that can contain/hold other controls in it. The contained controls are known as child

controls. 7 What happens to container control and its child controls if you delete

(i) The container control, (ii) A child control (iii) All child controls?

Ans: (i) If container control is deleted then with container control child control will be also deleted (ii) If a child control is deleted then the container control will be remain and a child control will be deleted. (iii) If all child controls is deleted then the container control will be remain and all the child control will be

deleted. 8 Name the character set supported by java. Ans: Java uses the Unicode character set. 9 What are keywords? Can keywords be used as identifiers? Ans: Keywords are the words that convey a special meaning to the language compiler. These are reserved for

special purpose. Keywords cannot be used as identifiers.

10 What is an identifier? What is the identifier forming rule of java? Ans: Identifiers are user define names for different parts of program.

Identifier forming rules of java state the following: 1. Identifiers can have alphabets, digits, and underscore and dollar sign characters. 2. They must not be a keyword or Boolean literal or null literal. 3. They must not begin with a digit. 4. They can be of any length. 5. Java is case sensitive i.e., upper-case letters and lower case letters are treated differently.

11 Is java case sensitive? What is meant by the term ‘case sensitive’? Ans: Java is case sensitive.

Case sensitive meant upper-case letters and lower case letters are treated differently. 12 Which of the following are valid identifiers and why/why not?

http://cbsecsnip.inPage 1 of 13

Page 2: IP Course - Java Tour Part 1 by Sumita Arora

Data_rec, _data, 1 data, data 1, my.file, asm, switch, goto, break

Ans: 1. Data_rec: Valid identifiers 2. _data: Valid identifiers 3. 1 data: Not valid because they must not begin with a digit 4. data 1: not valid because space is not allowed. 5. my.file: not valid because special character not allowed. 6. Asm: Valid identifiers 7. Switch: not valid because reserved keyword is not allowed. 8. goto: not valid because reserved keyword is not allowed. 9. break: not valid because reserved keyword is not allowed.

13 What are literals? How many types of integer literals are available in java? Ans: Literals are data items that never change their value during a program run.

integer-literal (decimal, octal, hexadecimal) 14 How many types of integer constant are allowed in java: How are they written? Ans: There are three types of integer constant are allowed in java.

1. Decimal: 17 2. Octal: 011 3. Hexadecimal: 0XBC1

15 What kind of constant are the following: 14, 011, 0X2A, 17, 014, 0XBC1? Ans: 1. Decimal: 14, 17

2. Octal: 011, 014 3. Hexadecimal: 0XBC1, 0X2A

16 Write the following real constants into exponent form: 23.197, 7.214, 0.00005, and 0.319. Ans: 1. 23.197 -> 2.3197 × 102 =2.3197E02

2. 7.214 -> 0.7214 x 101 = 0.7214E01 3. 0.00005 -> 0.000005 x 101 =0.000005E01 4. 0.319 -> 0.0319 x 101 = 0.0319E01

17 What will be the result of a=5/3 if a is (i) float (ii) int?

Ans: (i) float-> 1.0 (ii) int->1

18 The expression 8%3 evaluates to _____________. Ans: 2 19 What will be the value of j=--k+2*k+l++ if k is 20 initially? Ans: 57 20 What will be the value of p=p*++j where j is 22 and p=3 initially? Ans: 69 21 Write equivalent java expression for the following expressions:

(i) ut + 𝟏𝟏𝟐𝟐 ft2

(ii) |a|+b>=|b|+a

(iii) �𝟑𝟑𝟑𝟑+𝟓𝟓𝟓𝟓

𝟓𝟓𝟑𝟑+𝟑𝟑𝟓𝟓− 𝟖𝟖𝟑𝟑𝟓𝟓

𝟐𝟐𝟓𝟓𝟑𝟑�

(iv) 𝒆𝒆�𝟐𝟐𝒙𝒙𝟐𝟐−𝟒𝟒𝒙𝒙�

Ans: (i) x=u*t+(1/2)*f*t*t;

(ii) a+b>=b+a; (iii) x=((3*x+5*y)/(5*x+3*y)-(8*x*y)/(2*y*x)) (iv) Math.pow(e,(2*x*x-4*x))

http://cbsecsnip.inPage 2 of 13

Page 3: IP Course - Java Tour Part 1 by Sumita Arora

22 What is meant by implicit and explicit type conversion? Ans: An implicit type conversion is a conversion performed by the compiler without programmer’s intervention.

An explicit type conversion is user-defined that forces an expression to be of specific type. 23 What do you mean by type casting? What is type cast operator? Ans: The explicit conversion of an operand to a specific type is called Type Casting.

Type cast operator is used for Casts or converts a value to the specified type. 24 What will be the resultant type of the following expressions if bh represents a byte variable, i is an int variable, fl is a

float variable and db is a double variable? (i) Bh-i+db/fl-i*fl+db/i. (ii) (int) (fl+db)

Ans: (i) Double (ii) Double

25 What is the significance of null statement? Ans: A null statement is useful in those instances where the syntax of the language requires the presence of a statement

but where the logic of the program does not. 26 What are three constructs that govern statement flow? Ans: Sequence

Selection Iteration

27 One out of several different alternatives can be selected with the help of which statement? Ans: Switch statement 28 What is significance of a break statement in a switch statement? Ans: The break statements are necessary because without them, statements in switch blocks fall through: All statements

after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.

29 Write one limitation and one advantage of switch statement. Ans: Advantage:

The switch statement is a control statement that handles multiple selections and enumerations by passing control to one of the case statements within its body.

Limitation: Logical operators cannot be used with switch statement.

case k>=20; Switch case variables can have only int and char data type.

30 What is the effect of absence of break in a switch statement? Ans: without the break statement, statements in switch blocks fall through 31 What is the significance of default clause in a switch statement? Ans: Default clause is used for the else situation in a switch statement 32 What are iteration statements? Name the iteration statements provide by java. Ans: The iteration statements allow a set of instructions to be performed repeatedly until a certain condition is fulfilled.

Java provides three kind of looping statements: for loop, while loop, do-while loop. 33 Which elements are needed to control a loop? Ans: 1. Initialization Expression(s).

2. Test Expression. 3. Update Expression(s). 4. The Body-of-the-Loop.

34 What is meant by an entry-controlled loop? Which java loops are entry-controlled? Ans: Entry-controlled loops: Here loop condition is tested before entering in to the loop.

for and while loops are entry-controlled loop 35 What is meant by an exit-controlled loop? Which java loops are exit-controlled? Ans: Exit-controlled loops: Here loop body is executed first and then continuation condition is evaluated.

Do-while loop is exit-controlled loop.

http://cbsecsnip.inPage 3 of 13

Page 4: IP Course - Java Tour Part 1 by Sumita Arora

36 Write for loop that displays the numbers from 51 to 60. Ans: for(int i=51;i<60;i++) 37 Write for loop that displays the numbers from 10 to 1 i.e., 10, 9, 8…….. 3, 2, 1 Ans: for(i=10;i>=1;i--)

Type B: Short/Long Answer Questions 1 How are event, message, methods and properties interrelated? Ans: Event, message, methods and properties all are used for accomplishing the Event handling system for GUI application.

All 4 of them are interrelated with each other by this way – Event is occurrence of something happened, when event occurred message is sent to application based on which method takes an action which and depending on the method’s action properties are sets or gets.

2 Name at least three common controls. Also give some of their properties. Ans 1. Label

Properties: background, border, font, foreground, icon, text 2. TextField

Properties: background, border, font, foreground, editable, focusable, text 3. Password Field

Properties: background, font, foreground, text, echoChar 3 How are keywords different from identifiers? Ans:

Keywords Identifiers Keywords are reserved words used for

special purpose and must not be used as normal identifier names.

Example : do, if, catch, final

Identifiers are fumdamental building blocks of a program and are used as the general terminology for the names given to different parts of the program.

Example: Myfile, _ds, date_7_9

4 What are literals in java? How many types of literals are allowed in java? Ans: Literals are also referred to as constants. They never change their value during program run.

There are several kinds of literals available in Java. These are: integer-literals, character-literals, floating literals, boolean literals, string literals, the null literals.

5 Determine the data type of the expression (i) �𝟏𝟏𝟏𝟏𝟏𝟏(𝟏𝟏−𝐩𝐩𝐩𝐩)

(𝐩𝐩+𝐫𝐫) � − � (𝐩𝐩+𝐫𝐫)/𝐬𝐬(𝐥𝐥𝐥𝐥𝐥𝐥𝐥𝐥)(𝐬𝐬+𝐩𝐩)�

(ii) �𝟐𝟐𝒙𝒙+𝟑𝟑𝟑𝟑

𝟓𝟓𝟓𝟓+𝟔𝟔𝟔𝟔+ 𝟖𝟖𝟖𝟖

𝟓𝟓𝟓𝟓�4

If p, x is an int, r, w is a float, q, y is a long and s, z is double, t is short and u is long double.

Ans: (i) double (ii) double

6 Given the following code fragment: if (a == 0) System.out.println (“Zero”); if (a == 1) System.out.println (“One”); if (a == 2) System.out.println (“Two”); if (a == 3) System.out.println (“Three”); Write an alternative code (using if) that saves on number of comparisons.

Ans: if(a==0) { System.out.println(“Zero”); }

http://cbsecsnip.inPage 4 of 13

Page 5: IP Course - Java Tour Part 1 by Sumita Arora

else if(a==1) { System.out.println(“One”); } else if(a==2) { System.out.println(“Two”); } else if (a == 3) { System.out.println(“Three”); }

7 Rewrite the following fragment using switch: if (ch == ‘E’) eastern++; if (ch == ‘W’) western++; if (ch == ‘N’) northern++; if (ch == ‘S’) southern++; else unknown++;

Ans: switch(ch) { case‘E’: eastern++; break; case‘W’: western++; break; case‘N’: northern++; break; case‘S’: southern++; break; default: unknown++; }

8 How many times are the following loops executed? (a) X=5; Y=50;

while(X <= Y) { X = Y/X; ……….. }

(b) int m=10,n=7; while (m % n >=0) { . .. . . .. . . . . . .. m =m + 1; n = n + 2; …………… }

Ans: (a) Infinite loop (b) Infinite loop

http://cbsecsnip.inPage 5 of 13

Page 6: IP Course - Java Tour Part 1 by Sumita Arora

9 Given the following code fragment:

i = 2; do { System.out.println (i); i += 2; } while (i < 51); JOptionPane.showMessageDialog (null, “Thank you”); Rewrite the above code using a while loop.

Ans: i = 2; while (i < 51); { i += 2; System.out.println(i); } JOptionPane.showMessageDialog (null, “Thank you”);

10 Given the following code fragment: i = 100; while (i > 0) System.out.println (i--) JOptionPane.showMessageDialog (null, “Thank you”); Rewrite the above code using a do while loop.

Ans: i = 100; do{ System.out.println (i); i--; }while (i > 0) JOptionPane.showMessageDialog (null, “Thank you”);

11 Rewrite following while loop into a for loop int stripes = 0; while (stripes <= 13) { if (stripes % 2 == 2) { System.out.println(“colour code Red”); } else { System.out.println(“colour code Blue”); } System.out.println(“New stripe”); stripes = stripes + 1; }

Ans: int stripes = 0; for(stripes=0; stripes <= 13; stripes++) { if (stripes % 2 == 2) { System.out.println(“colour code Red”); } else { System.out.println(“colour code Blue”); } System.out.println(“New stripe”); }

http://cbsecsnip.inPage 6 of 13

Page 7: IP Course - Java Tour Part 1 by Sumita Arora

Output and Error Questions

12 Find the output of the following code fragments?

(a) int s = 14; if (s < 20) System.out.print("under"); else System.out.print("Over"); System.out.println("the limit.")

(b) int s = 14; if(s < 20) System.outprint("under"); else { System.out.print("over"); System.out.println("the limit"); }

(c) int s = 94; if(s < 20){ System.outprint("under"); } else { System.out.print("over"); } System.out.println("the limit");

Ans: (a) Compilation Error Correct Code int s = 14; if (s < 20) System.out.print("under"); else System.out.print("Over"); System.out.println("the limit.");

Correct Output-> underthe limit (b) Compilation Error

Correct Code int s = 14; if(s < 20) System.out.print("under"); else { System.out.print("over"); System.out.println("the limit"); } Correct Output-> underthe limit

(c) Output -> Overthe limit

13 What will be the output of following code fragment when the value of ch is (a) ‘A’ (b) ‘C’ (c) ‘D’ (d) ‘F’?

switch(ch) { case 'A' : System.out.println("Grade A"); case 'B' : System.out.println("Grade B"); case 'C' : System.out.println("Grade C"); break; case 'D' : System.out.println("Grade D");

http://cbsecsnip.inPage 7 of 13

Page 8: IP Course - Java Tour Part 1 by Sumita Arora

default : System.out.println "Grade F"; }

Ans: (a) When input is A, the output will be as follows: Grade A Grade B Grade C

(b) When input is C, the output will be as follows: Grade C

(c) When input is D, the output will be as follows: Grade D

(d) When input is F, the output will be as follows: Grade F

14 Predict the output of the following code fragment: int n=0; int j=7; int i=3; if(i>2) { n=1; if(j>4) n=2; else n=3; } else { n=4; if(j%2 >= 2) n=5; else n=6; } JOptionPane.showMessageDialog(null,n);

Ans:

15 Predict the output of the following code fragments:

(a) int i,j,n; n=0; i=1; do { n++; i++; } while(i<=5); (b) int i=1,j=0,n=0; while(i<4) { for(j=1;j<=i;j++) { n+=1; } i=i+1; } System.out.println(n); (c) int i=3,n=0;

http://cbsecsnip.inPage 8 of 13

Page 9: IP Course - Java Tour Part 1 by Sumita Arora

while (i<4) { n++; i--; } System.out.println(n); (d) int j=1,s=0; while(j<10) { System.out.println(j+ "+"); s=s+j; j=j+j%3; } System.out.println("=" +s);

Ans: (a) If System.out.print() is inside while loop output is 12345 and if outside of while loop output is 5 (b) 6 (c) -2147483644 (d) 2+

4+ 5+ 7+ 8+ =27

16 Find out errors, if any: (a) m=1; n=0; for(;m+n<19;++n) System.out.println("Hello \n"); m=m+10; (b) while(ctr !=10); { ctr=1; sum=sum+a; ctr=ctr+1; } (c) for(a=1, a>10;a=a+1) { ....... }

Ans: (a) int m,n; m=1;

n=0; for(;m+n<19;++n)

System.out.println("Hello \n"); m=m+10;

(b) while(ctr !=10) { ctr=1;

sum=sum+a; ctr=ctr+1; }

(c) for(a=1; a>10;a=a+1) { .......

} 17 Identify the possible error(s) in the following code fragment: Discuss the reason(s) of error(s) and correct the code.

:

http://cbsecsnip.inPage 9 of 13

Page 10: IP Course - Java Tour Part 1 by Sumita Arora

f=1; for(int a=40; (a); a--) f*=a; : s=0; for(int a=1;a<40/a++) s+=a;

Ans: : f=1; for(int a=40;a>1;a--) f*=a; : s=0; for(int a=1;a<40;a++) s+=a;

Type C: Practical/Lab Questions 1 Design an application that performs arithmetic operations (+, -, *, and /). The sample screenshot is being show

below.

Ans: // Addition

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int num1=Integer.parseInt(jTextField1.getText()); int num2=Integer.parseInt(jTextField2.getText()); jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())+Integer.parseInt(jTextField2.getText()))); } // Substraction private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { int num1=Integer.parseInt(jTextField1.getText()); int num2=Integer.parseInt(jTextField2.getText()); jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())-Integer.parseInt(jTextField2.getText()))); } // Multiply private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { int num1=Integer.parseInt(jTextField1.getText()); int num2=Integer.parseInt(jTextField2.getText()); jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())*Integer.parseInt(jTextField2.getText())));

http://cbsecsnip.inPage 10 of 13

Page 11: IP Course - Java Tour Part 1 by Sumita Arora

} // Division private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) { int num1=Integer.parseInt(jTextField1.getText()); int num2=Integer.parseInt(jTextField2.getText()); jTextField3.setText(Integer.toString(Integer.parseInt(jTextField1.getText())/Integer.parseInt(jTextField2.getText()))); } //Clear jTextField1.setText(null); jTextField2.setText(null); jTextField3.setText(null);

2 Practice – if statements. Design a GUI application having interface as shown below:

In the left half of above interface, two numbers are to be accepted. When the user clicks at button “Which

number is larger ?”, appropriate message gets displayed in the left label. In the right half of above interface, a number grade is input. Valid grades are 0-4. Upon clicking the right

push button, a message depicting validity of grade is displayed in right label. Ans: //larger number

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int num1=Integer.parseInt(jTextField1.getText()); int num2=Integer.parseInt(jTextField2.getText()); if(num1>num2) { jLabel4.setText("The first number is larger than the second"); } else { jLabel4.setText("The second number is larger than the first"); } } // valid number private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: float num1=Float.parseFloat(jTextField3.getText()); if(num1>=0.0 && num1<=4.0) { jLabel8.setText("This is a valid Grade"); }

http://cbsecsnip.inPage 11 of 13

Page 12: IP Course - Java Tour Part 1 by Sumita Arora

else { jLabel8.setText("This is NOT a valid Grade"); } }

3 Practice –Loops. Design a GUI application having interface as shown below:

The loop for the frame should print numbers from lowest to highest on the basis of given two numbers,

upon clicking at “Count” button. Ans: // loop count

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { int num1=Integer.parseInt(jTextField1.getText()); int num2=Integer.parseInt(jTextField2.getText()); String msg=""; if(num1<num2) { jLabel1.setText(Integer.toString(num1)); for(int i=num1;i<=num2;i++) { msg+=Integer.toString(i)+" "; } jLabel1.setText(msg); } else { jLabel1.setText(Integer.toString(num2)); for(int i=num2;i<=num1;i++) { msg+=Integer.toString(i)+" "; } jLabel1.setText(msg); } }

4 Practice –Select case. Design a GUI application having interface as shown below:

http://cbsecsnip.inPage 12 of 13

Page 13: IP Course - Java Tour Part 1 by Sumita Arora

The percentage marks are to be entered in the text box and upon clicking at the button, corresponding grade (as per following rules) should be displayed in the picture box below command button.

Ans private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: int num=Integer.parseInt(jTextField1.getText()); switch(num/10) { case 10: jLabel1.setText("You get an A++"); break; case 9: jLabel1.setText("You get an A++"); break; case 8: jLabel1.setText("You get an A+"); break; case 7: jLabel1.setText("You get an A"); break; case 6: jLabel1.setText("You get a B"); break; case 5: jLabel1.setText("You get a C"); break; case 4: jLabel1.setText("You get a D"); break; default: jLabel1.setText("Fail"); } }

http://cbsecsnip.inPage 13 of 13