Some Important Questions with Answers (Java, Netbeans)
Chapter 3,4,5,6,7,8
1. Which window
is used to designed the form.
Ans: Design window
Ans: Design window
2.
Which window contains the Swing Controls components. Ans: Palette
window
3.
What is the most suitable component to accept multiline text. Ans : Text Area
4.
What will be the output of the following command?
Learning.concat("Java")
Ans : Error
5.
What will be the output of the following command?
"Learning".concat("Java")
Ans:
LearningJava
6.
Name the different list type controls offered by Java Swing. Ans: (i) jListBox
(ii) jComboBox
7.
Name any two commonly used method of ListBox.
Ans: getSelectedIndex() and getSelectedValue()
Ans: getSelectedIndex() and getSelectedValue()
8.
Write code to add an element (“New Course”) to a list (SubList) at the
beginning of the list. Ans: SubList.add(0,”New Course”);
9.
Describe the three common controls. Also give some of their properties.
Ans:
Ans:
(i) jButton text,icon
(ii) jLabel text,border
(iii) jTextField text,font
10.
By default a combo box does not offer editing feature.How would you make a
combo box editable. Ans: By setting its editable property to
false.
11. Write Name the component classes of Swing API for
the following components-
(a) frame (b)
button
Ans:(a)JFrame(b)JButton
12. What is the name of event listener interface
for action events ? Ans: ActionPerformed
13.
What does getpassword() on a password field return ? Ans: a character array
14. What is event driven programming?
Ans:This programming style responds to the user events
and is driven by the occurrence of user events.
15. What are containers? Give examples.
Ans:
Containers are those controls inside them e.g., frame (JFrame), Panel (JPanel),
label (JLabel) etc. are containers.
16.
Which method of list is used to determine the value of selected item, if only
one it Ans: getSelectedValue()
17. Which type of storage is provided by variables.
Ans: temporary
Ans: temporary
18. What will be the output of the following code
segment:
String firstName = "Johua ";
String firstName = "Johua ";
String lastName = "Yacomo";
String
fullName = firstName + lastName;
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);
Ans: Full Name:
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);
Ans: Full Name:
JohuaYacomo
19.
Which expression is used to print the value of a variable "x" of type
int. Ans:
jTextField1.setText("x = " + x);
20. The statement i++; is equivalent to
Ans :i=i+1
Ans :i=i+1
21. Name the primitives datatypes in java.
Ans: Numeric type , Fractional type, Character type and
Boolean type.
22.
Which events gets fired when a user click a JButton and JRadioButton. Ans: ActionPerformed
23. Which of the following is a selection construct ?
a. do while Loop b. for Loop c. while Loop d.
None of these Ans:
none of these
24.
What will be used if there are two or more possible options. Ans: is else if
and switch
25. Name the loop that never ends.
Ans: infinite loop
Ans: infinite loop
26.
Which braces is used to enclose statements in a block statement. Ans: { } Curly
braces
27. Which of the following is an exit controlled loop?
a. for loop b. do while Loop
c. while loop d. none of these
Ans: do while loop
Ans: do while loop
28.Which
process is used to translate a task into a series of commands that a computer will use to perform that task.
Ans: Project design
29.
Which of the following component is the best suited to accept the country of
the
user?
user?
A. List
B. Combo box
C. Radio button
D. Check box
Ans: List and combo box
30.
Which construct will be used to find the sum of the first 10 natural numbers? Ans: for loop
31.
Which of the following is not a good programming guideline? Ans : Using text
fields to accept input of marital status
Short answers type questions:
1. Explain the following terms:
a) IDE
b) Form
Ans:
Ans:
a) IDE : IDE is an acronym for Integrated Development
Environment which is a work environment that integrates all tools necessary for
Application Development
and makes them available as part of one environment.
and makes them available as part of one environment.
b)
Forms: Forms are used to accept data (input) and submit data to an external
agent for processing.
2. Explain the usage of the following methods
:
a) setText()
b) toString()
c) concat()
Ans:
Ans:
a) setText() : It is used to change the display text of
a component (label, text field or button) during run time.
b) toString() :
It is used to convert an Integer value to String type.
c)
concat() : The concat() method or the string concatenation symbol(+) may be
used to add two strings together.
3. Differentiate between:
Ans:
a) Text field and Text area components :
The
Text Field allows the user to enter a single line of text only. But Text Area
component allows to accept multiline
input from the user or display multiple lines of information.
b) Text field and Password field components:
The Text Field displays
the obtained text in unencrypted form whreas password field displays the
obtained text in encrypted form. This component allows confidential input like
passwords which are single line.
c) parseInt() and parseDouble() methods:
parseInt()
is used to convert a string value to Integer type whereas parseDouble() is used
to convert a string value to type Double.
4. What is a
Variable?
Ans: Variables are named temporary storage locations.
5. Why are
data types important?
Ans:
Data Types define the way the values are stored, the range of the values and the operations that can be performed on that type.
6. How are keywords different from variable
names?
Ans:
Keywords have special meaning in java, should not be used as the variable
names. Variables are named temporary storage locations.
7. What is an identifier?
Ans:Identifiers
are fundamental building block of a program and are used as the general
terminology for the names given to different parts of the program viz.
variables, objects, classes,functions, arrays etc.
8. 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.
9. What is the purpose of break statement in
a loop?
Ans:In a loop, the break statement terminates the loop
when it gets executed.
10. Is Java case sensitive? What is meant by
case sensitive?
Ans:
Yes java is case sensitive. Case sensitive means upper case letters and lower
case letters are treated differently.
11. Is a string containing a single character
same as a char?
Ans: No
12. What is the main difference between a
combo box and a list box?
Ans:
The List Box does not have a text field the user can use to edit the selected
item, wheras a Combo Box is cross between a text field and a list.
13. Explain the use of for statement along
with its syntax.
Ans: The for loop repeat a
set of statements till a test condition is satisfied. The syntax of the for loop is:
Syntax
for(
initialization; test exp; increment/decrement exp)
{
{
statements;
}
}
14. What is the difference between selection
and repetition?
Ans: Selection statements
test conditions and selectively execute code depending on the outcome of the
test condition , whereas repetition statements repeat a set of statements till a test condition is satisfied.
test condition , whereas repetition statements repeat a set of statements till a test condition is satisfied.
15. What is the purpose of if statement?
Name the different forms of if statement.
Ans: The if statement allows selection (decision making)
depending upon the outcome of a condition.
a) simple if
b) if else
c) if - else if - else
16. What is the purpose of default clause in
a switch statement?
Ans: The default statement gets executed when no match
is found in switch.
17. What is the main difference between a
while loop and a do while loop?
Ans:
In while loop test expression is evaluated at the beginning of the loop whereas
in do while loop the test
expression is evaluated at the bottom of the loop. This means that do-while loop is executed at least once.
expression is evaluated at the bottom of the loop. This means that do-while loop is executed at least once.
18. 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.
19. Excessive comments add time to the
execution of your program. (True/False).
Ans: False because comments are non executable.
20. Differentiate between compile time and
run time errors.
Ans: Compile
time errors refer
to the errors
that violate the
grammatical rules and
regulations of programming
language.
21. Which error is harder to locate and why?
Ans: Logical errors
because in presence of logical error , the program executes without any
problems but the output produced is not correct. Therefore every statement of
the program need to be scanned.
22. Explain the following terms:
a) Exception
handling
: Run time
errors are also
called exceptions, and
handling such errors
in the application is called exception handling.
b) Syntax : Formal set of rules defined for writing any statement
in a language is known as syntax.
c) Portability : The application should be portable. It should be able
to run on different platforms.
d) Prettyprinting :
Prettyprinting is the
formatting of a
program to make
it more readable.
These
formatting conventions
usually consist of changes in positioning, spacing, color, contrast, size and
similar modifications intended to make the content easier to view, read and
understand.
e) Syntax error: Syntax errors occur when syntax rules of any programming
language are violated. These errors occur
during compilation of the application
OutPut
Finding Questions:
1 Write the output :
(i) System.out.printl(“Hello”.charAt(3));
(ii) System.out.printl(“Good morning”.substring(4));
Ans:
Ans:
(i) l
(ii) morning
2. Write the output of the following code :
intx , y = 0;
for(x=1;x<=5;++x)
y = x++;
y = x++;
--y ;
Ans: 7 4
3. Find the output of the code:
int f=1,i=2;
do
{
f*=i;
}while(++i<5);
System.out.println(f);
Ans: 24
4. What would the following code do :
Connection , statement created
String str=”select * from emp”;
Resultsetrs= stmt.executeQuery(str);
rs.last();
rs.last();
int r= rs.getRow();
JOptionPane.showMessageDialog(null,””+r);
Ans : if emp table has 5 records it will display 5
5. What will be
the output of the following code segment:
String firstName = "Johua ";
String lastName = "Yacomo";
String fullName = firstName + lastName;
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);
jTextField1.setText("Full Name: ");
jTextField2.setText (fullName);
Ans: Full Name :
JohuaYacomo
6. What will be the value of j and k after execution of
the following code:
int j=10,k=12;
if(k>=j)
{k=j;
J=k;
}
}
Ans: 10 10
7. How many times, the following loop gets
executed?
i=0;
while (i> 20)
{
{
//Statements
}
}
Ans: 0 times
8. How many times, the following loop gets executed?
i=0;
do
{
//Statements
}while (i> 20);
}while (i> 20);
Ans: 1 time
9. What will be the contents of jTextield1 and
jTextField2 after executing the following statement:
StringBuffer s= new StringBuffer(“Common Wealth”);
Int c=s.capacity();
s.insert(0,’E’);
s.reverse();
jTextField1.setText(“”+c);
jTextField2.setText(s.toString());
Ans:
Ans:
29
htlaeWnommoCE
10. What will be the contents of jTextield after
executing the following statement:
int num=4;
num=num+1;
if(num>5)
if(num>5)
jTextField1.setText(Integer.toString(num));
else
jTextField1.setText(Integer.toString(num*4));
Ans : 7
11. Find the output of the following code:
int First = 7;
int Second = 73;
First++;
First++;
if (First+Second> 90)
jlabel1.setText("value is 90 ");
else
else
jlabel1.setText("value is not 90 ");
Ans :value is not 90
Ans :value is not 90
12. Find the output
int Number1 = 7,Number2=8;
int Second = 73;
int Second = 73;
if (Number1>0 || Number2>5)
if (Number1>7)
if (Number1>7)
jTextField1.setText("Code Worked");
else
else
jTextField1.setText("Code MightWork");
else
else
jTextField1.setText("Code will not Work");
Ans :Code MightWork
Ans :Code MightWork
13. How many times will the following loop get executed?
x = 5;
y = 36;
while ( x <= y)
{
{
x+=6;
}
}
Ans: 6
14.
What will be the content of the jTextArea1 after executing the following code? Int Num = 1;
do
{
jTextArea1.setText(Integer.toString(++Num) +
"\n");
Num = Num + 1;
Num = Num + 1;
}while(Num<=10)
Ans: 10
Ans: 10
15. What will be the contents of jTextfield1 and
jTextfield2 after executing the following code:
String s=”KENDRIYA VIDYALAYA GUNA”
jtextfield1.setText(s.length()+” “);
jtextfield2.setText(Math.round(2.34)+“”);
Ans
: 23 2
16. What will be the value of s after executing the
following code?
double i,sum=2
for(i=3;i<8;++i)
{
if(i%4= =0)
{ break;
sum=Math.pow(sum,i);
}
}
else
sum+=i/2;
}
}
Ans: 150.0625
17.
What will be the content of jTextField1 and
jTextField2 after executing the following code:
String st=”New to Information Technology”;
jTextField1.setText(st.replace(“Technology”,”Practices”);
jTextField2.setText(st.substring(7));
Ans: New to Information Practices
Information Technology
18. Predict the
output for tan & tan1 if sac equals 7?
int tan = 0, tan1 = 4 ;
if ( sac == 2 )
if ( sac == 2 )
{
tan = 4 ; tan1 = 0; }
else if (sac == 8)
{ tan = 0 ; tan1 = 4; }
JOptionPane.showMessageDialog( null , “ tan = “ + tan +”
, tan1 = “ + tan1 ) ;
Ans: tan = 0 tan1=4
19. Give the output for the following code fragment:
v = 20 ;
do
do
{
JOptionPane.showMessageDialog(
null , v + “ ” ) ; } while ( v< 50 )
;
JOptionPane.showMessageDialog( null , “ Bye “ ) ;
Ans: Infinite loop
20. Give the
value of x after executing following Java code. Also find how many times the following loop will execute? :
int a=10;
int b=12;
int x=5;
int y=6;
int x=5;
int y=6;
while (a<=b)
{
if (a%2= =0)
x=x + y;
x=x + y;
else
x=x-y;
a=a+1;
}
Ans:11
21. What will be the output produced by following code
fragment? (1)
flaot x=9;
float y=5;
int z=(int)(x/y);
switch(z)
switch(z)
{
case1:x=x+2;
case2: x=x+3;
default:x =x+1;
}
case2: x=x+3;
default:x =x+1;
}
System.out.println(“value of x:”+x);
Ans: 15
22. Predict the output of the following code
fragments:
inti,j,n;
n=0;i=1;
do
do
{ n++; i++;
}
}
while(i<=5);
Ans: 5
Ans: 5
23. What will
be the output of the following program code when the user will press JButton:
Public class svm
{ int
a;
svm(int p)
{ a=p; }
void assign(int no)
{ a=no; }
intdisp()
{ return
a; }
}
private
void jButton1ActionPerformed(java.awt.event.ActionEventevt)
{
{
svm os=new svm(15);
System.out.println(“ “ + os.disp());
os.assign(35);
System.out.println(“ “ + os.disp());
}
Ans: 15 35
24. What will be the contents of jTextField1 and
jTextField2 after executing the following code:
String s = “Sun Micro Systems”;
jTextField1.setText(s.length()+””);
jTextField2.setText(s.toLowerCase());
Ans:
Ans:
jTextField1 : 17
jTextField2 : abc micro systems
25. What values will be
assigned to the variable ua ,ub, uc and fail after execution of the following program segment:
int i=0,ua=0,ub=0,uc=0,fail=0;
while(i<=5) {
while(i<=5) {
switch ( i++ )
{ case 1 :++ua;
case 2 : ++ub;
uc++;break;
case 3 :
case 3 :
case 4 : ++uc;
ua++;ub++;break;
default : ++fail;
default : ++fail;
}
Ans: ua=1
ub=1 uc=0
26. Predict an
output of the following code fragments:
int i = 1, j
= 0, n = 0;
while(i<4) {
while(i<4) {
for(j=1;j<= i ; j++)
{ n+=
1;
i = i+1;
}
System.out.println(n);
}
}
Ans : 6
27. Give the output of the following code:
int m=100;
while(m>0)
{
{
if (m<10)
break;
break;
m=m-10;
}
}
System.out.println(“m is “+m);
Ans: 0
Errors finding questions:
1. The
following code has some errors. Rewrite the corrected code .
int i=2,j=5;
while j>i
{
jTextField1.getText(“j is greater”);
j--; ++i;
j--; ++i;
}JOptionPane.showMessageDialog(“Hello”);
Ans:
Ans:
int i=2,j=5;
while( j>i)
while( j>i)
{
jTextField1.getText(“j is greater”);
j--; ++i;
j--; ++i;
}JOptionPane.showMessageDialog(“Hello”);
2. Identify
the errors :
switch(ch)
{ case
‘a’ :
case ‘A’
: case ‘e’ : case ‘E’ : case ‘i’ : case ‘I’ : case ‘u’ :
case ‘U’ : ++vowels;
break;
break;
default : ++others;
Ans: two case constants doesn’t have the same value
3. inti,j=5;
i==j+5;
if(i=j)
if(i=j)
{
jTextField1.setText(“i and j are unequal”);
jTextField2.setText(“they are not equal”); break;
}
}
else jTextField1.setText(“i and j are equal”);
Ans:
Ans:
int i,j=5;
i=j+5;
i=j+5;
if(i==j)
{
{
jTextField1.setText(“i and j are unequal”);
jTextField2.setText(“they are not equal”); break;
}
}
else jTextField1.setText(“i and j are equal”);
4. Rewrite the
code after making correction . Underline the corrections
int sum;value;inct;
int sum;value;inct;
int i
for(i==0;i<=10;i++)
sum=sum+i;
sum=sum+i;
inct++;
Ans :
int sum,value,inct;
int i;
int i;
for(i=0;i<=10;i++)
sum=sum+i;
sum=sum+i;
inct++;
5. The following code has some error(s). Rewrite the
correct code underlining all the corrections made.
int y=3;
switch(y);
{ case 1:
System.out.print(“Yes its One”);
case>2:
System.out.println(“Yes its more than Two”); break;
case else: System.out.print(“Invalid Number):
ans:
int y=3;
switch(y)
switch(y)
{ case 1:
System.out.print(“Yes its One”);
break;
case
2: System.out.println(“Yes its more than Two”); break;
default:System.out.print(“Invalid Number):
}
6. The following
has some error(s).Rewrite the correct code underlining all the corrections
made:
Inti,j=5;
i==j+5;
if(i=j)
if(i=j)
{
jtextfield1.setText(“I and j are unequal”);
jtextfield1.setText(“I and j are not equal”);breaks;
}
}
else
jtextfield1.setText(“I and j are equal”)
Ans:
inti,j=5;
i=j+5;
if(i==j)
{
i=j+5;
if(i==j)
{
jTextField1.setText(“I
and j are unequal”);
}
}
else
jTextField1.setText(“I and j are equal”)
7.
Rewrite the following Java code after underling the corrections made. int x = = 0;
int n= Integer parseInt(Jlabel1.getText);
Ans:
Ans:
int n= Integer parseInt(JLabel1.getText());
int x = 0;
8. Find out errors if any:
M=1;
N=0;
For(;m+n<19;++n)
System.out.println(“hello”);
M=m+10;
M=m+10;
Ans:
m=1;
n=0;
n=0;
for(;m+n<19;++n)
System.out.println(“hello”);
m=m+10;
m=m+10;
9. The following code has some error(s). Rewrite the
correct code underlining all the corrections made.
int y=6,p;
do
{ y=3.14*y;
p=y%10;
if p=2
if p=2
System.out.print(“Two”);
while(y>1)
while(y>1)
Ans:
int y=6,p;
do
do
{ y=3.14*y;
p=y%10;
if (p==2)
p=y%10;
if (p==2)
System.out.print(“Two”);
}while(y>1);
}while(y>1);
Rewrite questions:
1. Rewrite
the following program code using a for loop:
inti,sum=0;
while(i<10)
{ sum +=i;
i+=2;
{ sum +=i;
i+=2;
}
Ans: int i,sum=0;
for(i=0;i<10;i+=2)
{ sum +=i;
{ sum +=i;
}
2. Rewrite
the following code using while loop :
inti,j;
for(i=1;i<=4;i++)
{ for(j=1;j<=i;++j)
{ System.out.print(j);
}
System.out.println();
}
}
Ans:
int i=1,j;
while(i<=4)
{ j=1;
{ j=1;
while(j<=i)
{ System.out.print(j);
++j;
} i++;
System.out.println();
}
3. Write
a equivalent while loop for the following code:
intsz=25;
for(int i=0,sum=0;i<sz;i++)
sum+=i;
sum+=i;
System.out.println(sum);
Ans: int
sz=25;
int i=0,sum=0;
while(i<sz)
{
{
sum+=i;
i++;
i++;
}
System.out.println(sum);
4. Rewrite the
following if-else segment using switch-case statement
char ch='A';
char ch='A';
if(ch=='A')
System.out.println("Account");
if((ch=='C') || (ch=='G'))
System.out.println("Admin");
if(ch=='F')
if(ch=='F')
System.out.println("Advisor");
Ans: char
ch='A';
switch(ch)
{
{
case ‘A':
System.out.println("Account");
break;
break;
case 'C':
case 'G’:
case 'G’:
System.out.println("Admin");
break;
break;
case 'F':
System.out.println("Advisor");
}
5. Rewrite the
following code using while loop:
inti,j;
inti,j;
for(i=1,j=2;i<=6;i++,j+=2)
System.out.println(i++);
System.out.println(i++);
System.out.println(“Finished!!!”);
Ans: inti=1,j=2;
while(i<=6)
{System.out.println(i++);
i++;
i++;
j+=2;}
System.out.println(“Finished!!!”);
6. Rewrite
the following code using for loop.
int
i=0;
while(++i<20)
{
if( i==8)
break;
System.out.println(i++);
}
Ans:
int i;
for(i=1;i<20;++i)
{ if( i==8)
{ if( i==8)
break;
System.out.println(i++);
}
}
7. Rewrite the code using switch statement:
If(k==1)
Day=”Monday”;
elseif(k==2)
elseif(k==2)
Day=”Tuesday”;
elseif(k==3)
elseif(k==3)
Day=”Wednesday”;
else
Day=”-”
Ans:
switch(k)
{
{
case 1: Day=”Monday”;
break;
break;
case 2: Day=”Tuesday”;
break;
break;
case 3: Day=”Wednesday”;
break;
break;
default: Day=””;
}
}
8. Write the equivalent switch case for the following
code :
If (num1
= =1 )
jTextField1.setText(“Number is one”);
else If (num1 = =2 )
else If (num1 = =2 )
jTextField1.setText(“Number is two”);
else If (num1 = =3 )
else If (num1 = =3 )
jTextField1.setText(“Number is three”);
else
else
jTextField1.setText(“Number is more than three”);
Ans:
Ans:
Switch(num1)
{
{
Case 1 :
jTextField1.setText(“Number is one”);
break;
break;
case 2 :
jTextField1.setText(“Number is two”);
break;
break;
case 3 :
jTextField1.setText(“Number is three”);
break;
break;
default:
jTextField1.setText(“Number is more than three”);
}
}
9. Given the
following code fragment :
If(a==0)
System.out.println(“zero”);
If(a==1)
If(a==1)
System.out.println(“one”);
If(a==2)
If(a==2)
System.out.println(“two”);
If(a==3)
If(a==3)
System.out.println(“three”);
Write an alternative code (Using if) that saves on
number of comparsons
Ans:
if(a==0)
System.out.println(“zero”);
else if(a==1)
System.out.println(“one”);
else if(a==2)
else if(a==2)
System.out.println(“two”);
else if(a==3)
System.out.println(“three”);
10. Rewrite the following code fragment using switch :
if(ch = = ‘E’)
east++;
if(ch = = ‘W’)
west++;
if(ch = = ‘N’)
north++;
if(ch = = ‘S’)
south++;
else
jOptionPane.showMessageDialog(null, “unknown”);
Ans:
Switch(ch)
{
{
Case ‘E’:
east++;
break;
east++;
break;
case ‘W’:
west++;
break;
west++;
break;
case ‘N’:
north++;
break;
north++;
break;
case ‘S’:
south++;
break;
south++;
break;
default :
jOptionPane.showMessageDialog(null, “unknown”);
}
}
11 Rewrite the
following code using for loop:
int i = 0;
while(++i <20)
{
{
if(i = = 8)
break;
break;
System.out.println(++i);
}
Ans:
for(int
i = 0;++i <20;)
{
{
if(i = = 8)
break;
break;
System.out.println(++i);
}
No comments:
Post a Comment