Sunday, 5 February 2017

QUESTION ANSWERS

Very Short answers types questions 1. Write command to display a message dialog to display prompt as “Hello World” , title as “My dialog” and icon as question icon. Ans: JOptionPane.showMessageDialog(null,”HelloWorld”,”My dialog”, ); 2. Name the different list type controls offered by Java Swing. Ans: (i) jListBox (ii) jComboBox 3. Name any two commonly used method of ListBox. Ans: getSelectedIndex() and getSelectedValue() 4. Write code to add an element (“New Course”) to a list (SubList) at the beginning of the list. Ans: SubList.add(0,”New Course”); 5. What is difference between ‘a’ and “a” ? Ans: ‘a’ is character and “a” is string. 6. How would you make a combo box editable? Ans: By setting its editable property to true. 7. Write the expression to print the value of a variable "x" of type int in a label. Ans: jLabel1.setText(“”+x); 8. In JDBC coding, what methods would be opted to move to last record of the recordSetrecSet? Ans: recSet.last(); 9. What is the name of event listener interface for action events ? Ans ActionPerformed 10. Name the inheritance type which is not supported by JAVA. Ans Multiple inheritance 11. What will be the value of jTextField1 after execution of following code : jTextField1.setText(“Informatics”.subString(2,6)); Ans: form 12. Name the character set supported by Java. Ans: Unicode. 16. What will be the value of y if x has 42 (1) y= ++x (2) y= x++ Ans: (1) 43 (2) 42 17. Name the 4 essential class libraries that we need to import for setting up the connection with the database and retrieve data from the database. Ans: DriverManager, Connection, Statement, ResultSet 18. What is Event? Ans. An Event refers to the occurrence of an activity. 19. What is Message? Ans. A Message is the information/request sent to the application. 20. Which Swing control is invisible on the Frame? And: ButtonGroup 21. Which property of list box is used to display values in the list? And: Model Property 22. How one can make a text field un-editable on a frame? And: jTextfield1.setEditable(false) 23. Which will be displayed in jTextArea after executing the following? jTextArea1.setText(“India \n is a great \t country”); And: India Is a great country 24. How one can make a text field un-editable on a frame? And: jTextfield1.setEditable(false) 25. Predefined classes are available in the form of …………. And: package :: 49 :: Short answers type questions Q1. How GUI application works? Ans. Graphical User Interface (GUI) based application contains Windows, Buttons, Text boxes, dialogue boxes and Menus etc. known as GUI components. While using a GUI application, when user performs an action, an Event is generated. Each time an Event occurs, it causes a Message which sent to OS to take action. Q2. What is a Method (Function)? Ans. A Method or function is sequence of statement which is written to perform a specific job in the application. In Object Oriented Programming, Method represents the behavior of the object. A message can also be thought as a call to an object’s method. Q3. What is Event? How the computer responds to an event? Ans. An event is occurrence of some activities either initiated by user or by the system. Hence Any user action related to the GUI is called an event, Almost all actions the user performs will ”trigger” an event for us to handle. For example:  Moving the mouse  Clicking on a button  Writing text in a text box etc. In order to react, you need to implement some Event handling system in your Application. Three things are important in Even HandlingEvent Source: It is the GUI component that generates the event, e.g. Button. Event Handler or Event Listener: It is implemented as in the form of code. It receives and handles events through Listener Interface. Event Object or Message: It is created when event occurs. It contains all the information about the event which includes Source of event and type of event Q4. How to use Event Handlers in NetBeans? Ans. As you attached an Event along with Listener, you will find a code window along with prototyped method to perform actions defined by you. You may write commands to be executed in //TODO section. You may define Action Event, Item Event, Mouse Event, Key Event and Mouse Motion Event to a button. Generally, ActionPerformed() Event is handled likejButton1.ActionPerformed( java.awt.event.ActionEvent evt) You can also rename the Event handler method. Q5. Explain Text Fields, List, Combo Box , check box and radio control in Java? Ans: 1. TextField Text Field allow the user to enter data as single line text or display text data to the user. Various things we can do with Text Field (Methods)  A text field can be ”enabled” or ”disabled” • Enabling a text field : (Enabled means - Data can be entered ) How to do that- jTextField1.setEditable(true); • Disabling a text field : (Disabled: Data can only be displayed) How to do that- jTextField1.setEditable(false);  Data can be written into or Read from Text Fields • Setting the text in a text field ( Show what you want to display) How to do that- jTextField1.setText("Welcome User!"); • Getting the text from a text field (Get values into some variable) How to do that- String s = theTextField.getText(); 2. List box / Combo box A list (or combo) box enables the user to choose an option between many alternatives • List box: User can only choose between specified alternatives :: 50 :: • Combo box: User can either choose between specified alternatives, or specify choice manually (by typing it in) Various things we can do with List/Combo Box (Methods)  A List/Combo box can be ”enabled” or ”disabled” like a Text Field • Enabling a List/Combo box How to do that - theBox.setEditable(true); • Disabling a List/Combo box How to do that - theBox.setEditable(false);  Items can be selected programatically or Selected items can be read from them • Setting the selection in a List/Combo box How to do that - theBox.setSelectedItem(”Three"); • Getting the selection from a List/Combo box How to do that - String s = (String)theBox.getSelectedItem(); 3. Check boxes/ Radio Buttons Unlike list/Check Boxes, in some cases, the set of possible choices is limited to two options, Often a case of either/or, or perhaps on/off – such case best suits check boxes or radio buttons A Check box can only be in two states; checked or unchecked, it is very Nice fit for binary choices Various things we can do with List/Combo Box (Methods)  A Check Box/Radio Button can be ”enabled” or ”disabled” like a Text Field • Enabling a Check box/Radio Button How to do that - theRCBox.setEnabled(true); • Disabling a Check box/Radio Button How to do that - theRCBox.setEnabled(false);  Items can be selected programatically or Selected items can be read from them • Setting the selection in a Check box/Radio Button How to do that - theRCBox.setSelected(isSelected); • Getting the selection from a Check box/Radio Button How to do that - boolean isSelected = theCBox.isSelected(); Q6. What is a variable? Explain with example. Ans. A variable is named memory location, which holds a data value of a particular data type. Declaration and Initialization of variable- ; Example: int age; double amount; double price=214.70, discount =0.12; String name=“Amitabh” long x=25L; byte a=3; float x= a+b; By default all Numeric variables initialized with 0, and character and reference variable with null, boolean with false, if it is not initialized. The keyword final can be used with variable declaration to indicate constant. E.g. final double SERVICE_TAX=0.020 Q7. What do you mean by parsing ? Ans: The Parsing refers to converts textual data from GUI component in to numeric type. Byte.parseByte(String s) – string into byte. Short.parseShort(String s) – string into short. Integer.parseInt(string s) – string into integer. Long.parseLong(string s) – string into long. Float.parseFloat(string s) – string into float. Double.parseDouble(string s) – string into double. e.g. int age=Integer.parseInt(jTextField1.getText());

Q8. How to Display Dialogue Boxes in JAVA GUIs (Netbeans)? Ans: In GUI application often we require to display a message in the Dialog Boxes containing OK button to close the Dialog Box. The following steps can be used to display a message in a dialog box. Firstly, you need to import jOptionPane swing control at the top of program code, by typing – import javax.swing.JOptionPane; When required you may display a message by following code in a methodJOptionPane.showMessageDialog(null, “Hello.. “); Q9. Explain the concept of Focus. Ans: Focus is the ability to receive user input/ response through Mouse or Keyboard. When object or control has focus, it can receive input from user.An object or control can receive focus only if its enabled and visible property is set to true. Most of the controls provides FOCUS_GAINED() and FOCUS_LOST() method in FocusEvent by the FocusListener. FOCUS_LOST() is generally used for validation of data. You can give focus to an object at run time by invoking the requestFocus() method in the code. Example : jTextField1.requestFocus(); - This method puts focus on jTextField1. Q10. What is an expression? What are the various types of expressions in Java? Ans: An expression is a valid combination of operators, constants and variable and keywords i.e. combination of Java tokens. In java, three types of expressions are used. Arithmetic Expression: Arithmetic expression may contain one or more numeric variables, literals and operators. Two operands or operators should not occur in continuation. e.g. x+*y and q(a+b-z/4) is invalid expressions. Compound Expression: It is combination of two or more simple expressions. e.g. (a+b)/(c+d) and (a>b)||(by (y+z)>=(x/z) x||y && z (x) (x-y) Q11. What is ‘Scope’ of a variable? Explain. Ans: In Java, a variable can be declared anywhere in the program but before using them. The area of program within which a variable is accessible, is known as its scope. A variable can be accessed within the block where it is declared. Q12. What are Access Specifiers ? How Access is controlled for members of Super class? Ans. Access specifier tells a complier about the usability of a data member of a class in a java program. Java supports basically three types of access specifier, however there are some others too, these are: Public, Private, Protected, and further - default and private protected. • Public: A Class member with public access specifier is usable outside the class. i.e. it can be used in any class in the program. • Protected: A class member with protected access specifier can be inherited by a child class but is not usable outside the parent class. • Private: Private members of a class can just be utilized inside the class and are hidden outside the class a private member cannot be used in any other class other than the class in which it is declared. • Default: These members are accessible only in the class that are in the same package class i.e., in their own classes. • Private Protected: These members are accessible only from subclasses whether in the same package or in the other package. Q13. What is casting? When do we need it? Ans: Casting is a conversion, which uses the cast operator to specify the type name in parenthesis and is placed in front of the value to be converted. For example: Result = (float) total / count ; They are helpful in situations where we temporarily need to treat a value as another type. Q14. What is the purpose of break statement in a loop? Ans: In a loop, the break statement terminates the loop when it gets executed. :: 52 :: Q15. How is the if…else if combination more general than a switch statement? Ans: The switch statement must be by a single integer control variable, and each case section must correspond to a single constant value for the variable. The if…else if combination allows any kind of condition after each if. Q16. What is a container component? Ans: A container is a special type of component that can hold other components. Some Swing Containers are jPanel, jFrame, jApplet, jWindow, jDialog and jInternalFrame. The components contained in a container are called child component. Q17. How are protected members different from public and private members of a class? Ans: Protected members can be directly accessed by all the classes in the same package, as that of the class in which the member is and sub classes of other package. Whereas private members cann ot be accessed outside the class, even in subclasses of the class and public members can be directly accessed by all other classes. Q18. What is an abstract class and abstract method? Ans: An Abstract Class is the one that simply represents a concept and whose objects can’t be created. It is created through the use of keyword abstract. Abstract methods are methods with no method statements. Subclasses must provide the method statements for the inherited abstract methods e.g. in the following code class. Q19. Differentiate between JDBC and ODBC? Ans: JDBC (Java Database Connectivity) is developed by Sun Java for the purpose of connecting java applications with a variety of relation database systems like MySQL or Oracle. On the other hand, ODBC (open database connectivity) is a system developed by Microsoft to connect Microsoft based programming application (like visual basic) with a variety of relation databases. Q20. What are the main tasks of JDBC? Ans: Mainly JDBC perform the following: a) Establishes a connection with a relation database b) Sends SQL queries/ statements to the database c) Process the results obtained from the database server. Programming Problems 1. Write a java program to calculate the sum of all the no. divisible by 5 in the range 1 to 50. Ans: int sum=0; for(int i=1;i<=50;++i) { if(i%5==0) sum=sum+i; } jLabel1.setText(“”+sum); 2. What do you mean by infinite loop. Write one program that has infinite loop Ans: A loop that never terminates is called infinite loop. Example : for(;;) { jLabel1.setText(“Hello”);} 3. Write method in java that takes a number returns the sum of its digits. Ans int sumdig(int n) { int sum=0, r; while(n!=0) {int r=n%10; n=n/10; sum=sum+r; } return (sum); } 4. How many times, the following loop gets executed? :: 53 :: i=0; while(i>20) {//Statements } Ans: 0 times 5. How many times, the following loop gets executed? int i=0; do { //Statements }while(i>20); Ans: 1time Output Finding Questions 1. Write the output : (i) jTextField1.setText(“Hello”.charAt(3)); (ii) jTextField1.setText(“Good morning”.substring(4)); Ans: (i) l (ii) morning 2. Write the value stored on y variable after executing the following code : int x , y = 0; for(x=1;x<=5;++x) y=x++; Ans: 5 3. Find the output of the code: intf=1,i=2; do {f*=i; }while(++i<5); jTextField1.setText (“”+f); Ans: 24 4. What will be the value of j and k after execution of the following code: intj=10,k=12; if(k>=j) {k=j; J=k;} Ans: 10 10 5. What will be the contents of jTextield after executing the following statement: int num=4; num=num+1; if(num>5) jTextField1.setText(Integer.toString(num)); else jTextField1.setText(Integer.toString(num*4)); Ans: 20 6. Find the output of the following code: intFirst=7; intSecond=73; First++; if(First+Second>90) jlabel1.setText("valueis90"); else jlabel1.setText("valueisnot90"); Ans: value is not 90 :: 54 :: 7. Find the output int Number1=7,Number2=8; int Second=73; if(Number1>0||Number2>5) if(Number1>7) jTextField1.setText("CodeWorked"); else jTextField1.setText("CodeMightWork"); else jTextField1.setText("CodewillnotWork"); Ans: CodeMightWork 8. What will be the content of the jTextArea1 after executing the following code? intNum =1; do { jTextArea1.setText(Integer.toString(++Num)+"\n"); Num= Num + 1; }while(Num<=10);

Ans: 10 9. What will be the contents of jTextfield1 and jTextfield2 after executing the following code: Strings=”KENDRIYAVIDYALAYA SANGATHAN” jTextField1.setText(s.length()+””); jTextField2.setText(Math.round(2.34)+“”); Ans: 282 10. Give the value of x after executing following Java code. int a=10,b=12,x=5,y=6; while(a<=b) { if(a%2==0) x=x+y; else x=x-y; a=a+1; } Ans: 11 11. What will be the output produced by following code fragment? flaot x=9, y=5; int z=(int)(x/y); switch(z) { case1: x=x+2; case2: x=x+3; default: x=x+1; } System.out.println(“valueof x:”+x); Ans: value of x: 15 12. What values will be assigned to the variable ua ,ub, uc and fail after execution of the following program segment: inti=0,ua=0,ub=0,uc=0,fail=0; while(i<=5){ switch(i++) { case1:++ua; case2:++ub; uc++; break; case3: case 4:++uc; :: 55 :: ua++; ub++; break; default:++fail; } Ans: ua=1 ub=1 uc=0 13. What will be the contents of jTextField1 and jTextField2 after executing the following code: Strings=“SunMicroSystems”; jTextField1.setText(s.length()+””); jTextField2.setText(s.toLowerCase()); Ans: jTextField1:17 jTextField2: sunmicrosystems 14. Give the output of the following code: int m=100; while(m>0) { if(m<10)break; m=m-10; } System.out.println(“mis”+m); Ans: m is 0 Errors finding and conversion questions: 1. Thefollowingcodehassomeerrors.Rewritethecorrectedcode. inti=2, j=5; whilej>i { jTextField1.getText(“jisgreater”); j--;++i; } JOptionPane.ShowMessageDialog(“Hello”); Ans: int i=2,j=5; while( j>i) { jTextField1.getText(“j is greater”); j--; ++i; } JOptionPane.showMessageDialog(“Hello”); 2. Rewrite the code after making correction. int sum; value; inct; intifor(i==0;i<=10;i++) sum=sum+i; inct++; Ans: int sum, value, inct; for(int i=0;i<=10;i++) sum=sum+i; inct++; 3. The following code has some error(s). Rewrite the correct code. inty=3; switch(y); {case1:System.out.print(“YesitsOne”); case>2:System.out.println(“YesitsmorethanTwo”);
break; case else: System.out.print(“InvalidNumber): } Ans: inty=3; switch(y) {case1:System.out.print(“YesitsOne”); break; case2:System.out.println(“YesitsmorethanTwo”); break; default: System.out.print(“InvalidNumber); } 4. Find out errors and rewrite the code: M=1;N=0; For(;m+n<19;++n) System.out.println(“hello”); M=m+10; Ans: m=1;n=0; for(;m+n<19;++n) System.out.println(“hello”); m=m+10; 5. Rewrite the following program code using a for loop: int i,sum=0; while(i<10) {sum+=i;i+=2; } Ans: inti, sum=0; for(i=0;i<10;i+=2) {sum+=i; } 6. Rewrite the following code using while loop : int i,j; for(i=1;i<=4;i++) {for(j=1;j<=i;++j) { System.out.print(j); } System.out.println(); } Ans: inti=1, j=0; while(i<=4) { j=1; while(j<=i) { System.out.print(j); ++j; } i++; System.out.println(); } :: 57 :: 7. Rewrite the following if-else segment using switch-case statement. charch='A'; if(ch=='A')System.out.println("Account"); if((ch=='C')||(ch=='G'))System.out.println("Admin"); if(ch=='F')System.out.println("Advisor"); Ans: charch='A';s witch(ch) { case‘A':System.out.println("Account");break; case'C': case'G’: System.out.println("Admin");break; case'F': System.out.println("Advisor"); } 8. Rewrite the following code using while loop: int i,j; for(i=1,j=2;i<=6;i++,j+=2) System.out.println(i++);System.out.println (“Finished!!!”); Ans: inti=1,j=2;whi le(i<=6) {System.out.println(i++); i++; j+=2;} System.out.println(“Finished!!!”); 9. Rewrite the following code using for loop. int i=0; while(++i<20) { if( i==8) break; System.out.println(i++); } Ans: inti; for (i=1;i<20;++i) { if( i==8)break; System.out.println(i++);} 10. Write the equivalent switch case for the following code : if(num1==1) jTextField1.setText(“Numberisone”); elseIf(num1==2) jTextField1.setText(“Numberistwo”); elseIf(num1==3) jTextField1.setText(“Numberisthree”); else jTextField1.setText(“Numberismorethanthree”); Ans: Switch(num1) { case1:jTextField1.setText(“Numberisone”);break; case2: jTextField1.setText(“Numberistwo”);break;

case3: jTextField1.setText(“Numberisthree”);break; default: jTextField1.setText(“Numberismorethanthree”); } 11. Write an alternative code(Using if)of given code that saves on number of comparisons. 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”); Ans: if(a==0)System.out.println(“zero”); else if(a==1)System.out.println(“one”); else if(a==2)System.out.println(“two”); else if(a==3)System.out.println(“three”); Application Design Questions: Q1: Design an application for Theatre Booking system and answer the following questions?
What is the output
int a = 10; int b = 20; int c = 25; int d = 25; System.out.println("a + b = " + (a + b) ); System.out.println("a - b = " + (a - b) ); System.out.println("a * b = " + (a * b) ); System.out.println("b / a = " + (b / a) ); System.out.println("b % a = " + (b % a) ); System.out.println("c % a = " + (c % a) ); System.out.println("a++ = " + (a++) ); System.out.println("b-- = " + (a--) );

int a = 10; int b = 20; System.out.println("a == b = " + (a == b) ); System.out.println("a != b = " + (a != b) ); System.out.println("a > b = " + (a > b) ); System.out.println("a < b = " + (a < b) ); System.out.println("b >= a = " + (b >= a) ); System.out.println("b <= a = " + (b <= a) );

boolean a = true; boolean b = false; System.out.println("a && b = " + (a&&b)); System.out.println("a || b = " + (a||b) ); System.out.println("!(a && b) = " + !(a && b));

int a = 10; int b = 20; int c = 0; c = a + b; System.out.println("c = a + b = " + c ); c += a ; System.out.println("c += a = " + c ); c -= a ; System.out.println("c -= a = " + c ); c *= a ; System.out.println("c *= a = " + c );

a = 10; c = 15; c /= a ; System.out.println("c /= a = " + c ); a = 10; c = 15; c %= a ; System.out.println("c %= a = " + c ); c <<= 2 ; System.out.println("c <<= 2 = " + c );


int x = 30; int y = 10; if( x == 30 ){ if( y == 10 ){ System.out.print("X = 30 and Y = 10"); }

No comments:

Post a Comment