• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Marcus G exam 2 -33,48

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
33. What will happen when you attempt to compile and run this code
//Demonstration of event handling
import java.awt.event.*;
import java.awt.*;
public class MyWc extends Frame implements WindowListener{
public static void main(String argv[]){
MyWc mwc = new MyWc();
}
public void windowClosing(WindowEvent we){
System.exit(0);
}//End of windowClosing
public void MyWc(){
setSize(300,300);
setVisible(true);
}
}//End of class
why there would be a complier error occur??
48. Given the following variables
char c = 'c';
int i = 10;
double d = 10;
long l = 1;
String s = "Hello";
Which of the following will compile without error?
1)c=c+i;
2)s+=i;
3)i+=s;
4)c+=s;
the ans is 2, but why 3 and 4 not??

thanks~~~
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear May,
Here are the Answers provided by Marcus himself.
Answer 33)
Objective 4.6)
1) Error at compile time
If you implement an interface you must create bodies for all methods in that interface. This code will produce an error saying that MyWc must be declared abstract because it does not define all of the methods in WindowListener. Option 4 is nonsense as comments can appear anywhere. Option 3 suggesting that it might compile but not produce output is ment to mislead on the basis that what looks like a constructor is actually an ordinary method as it has a return type.
Answer 48)
Objective 5.1)
2)s+=i;
Only a String acts as if the + operator were overloaded

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first problem gives a compile error because the class implements the WindowListener interface without implementing all the interface's methods. (There are 7 and only windowClosing is implemented).
The second part had me puzzled too. After much thought it occurred to me that for 3 you are concatenating an int and a String but then trying to assign the String into an int. 4 is incorrect for the same reason except that it is being assigned to a char. Hope this helps.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ques 1:
You must implement all the methods in WindowListener. Since here only windowClosing is implemented compiler gives error.
Ques 2:
i+=s
expands to
i = (int) (i+s);
the result of (i+s) is a String reference as s is a string. This reference cannot be cast to a int. so 3 is wrong and 4 is wrong similarly.
-Aj
 
reply
    Bookmark Topic Watch Topic
  • New Topic