Download presentation
Presentation is loading. Please wait.
Published bySandra Baker Modified over 8 years ago
1
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora
2
Output? Public class Test { Test t = new Test(); public int show(){ return (true ? null : 0); } public static void main(String[] args) { Test a = new Test(); System.out.println(a.show()); }
3
Output? public class A{ public static void show(){ System.out.println("Static method called"); } public static void main(String[] args) { A obj=null; obj.show(); }
4
Increment Decrement int a, b; a=10; b=++a; System.out.println(b); a=10; b=a++; System.out.println(a);
5
Increment Decrement int a=20; a = a++ + 10; System.out.println(a); int a=20; a = ++a + 1; System.out.println(a);
6
Increment Decrement int a=20; a= a++ + a++; System.out.println(a); int a=20; a= a++ + ++a; System.out.println(a);
7
Output? Integer i1 = 128; Integer i2 = 128; System.out.println(i1 == i2);
8
Output? String s = “ONE”+3+2+”TWO”+5; System.out.println(s);
9
What happens? Integer I = new Integer(null); String S = new String(null);
10
Static Method Overriding public class CanWeOverrideStaticMethod { public static void main(String args[]) { Screen scrn = new ColorScreen(); scrn.show(); } class Screen{ public static void show(){ System.out.printf("Static method from parent class"); } } class ColorScreen extends Screen{ public static void show(){ System.err.println("Overridden static method in Child Class in Java"); } }
11
finally 1.What will happen if you put return statement on try or catch block ? Will finally block execute? 2.What will happen if you put System.exit () on try or catch block ? Will finally block execute?
12
Output? public static void main(String args[]) { try { int a=2/0; System.exit(0); } catch(Exception e) { System.out.println("i am in catch block"); } finally{ System.out.println("finally");} }
13
Output? public static int getMonthsInYear(){ try{ throw new RuntimeException(); } finally { return 12; }
14
Output? public static int test() { int i = 0; try { i = 2; return i; } finally { i = 12; System.out.println("finally trumps return."); }
15
Exceptions and Inheritance If a method throws NullPointerException in super class, can we override it with a method which throws RuntimeException?
16
compareTo vs equals "foo".equals((String)null) "foo".compareTo((String)null)
17
What will be the output of the program? public class TestDogs { public static void main(String [] args) { Dog [][] theDogs = new Dog[3][]; System.out.println(theDogs[2][0].toString()); } class Dog { }
18
Casting int x = 42; float f = 42.1f; if(x == f) System.out.println(…); if(f == x) System.out.println(…);
19
Which of the following are legal int $1234; int xy+abc; int 1alpha;
20
Any compilation errors? char c; int i; c = 'A'; i = c; c = i + 1 c++;
21
Method Overriding class A{ void method(int i) {} } class B extends A{ @Override void method(Integer i) {} }
22
Access Modifier class Fruit { protected static String name = "Sue"; } class Apple extends Fruit { } public class Application { public static void main(String[] args) { System.out.println(Apple.name); }
23
Nested Classes – Static Vs Non-Static (Inner) public class A { public static int name = 10; public class Constants { public static int name = 10; } An instance of an inner class has access to all of the members of the outer class, even those that are marked “private”. static inner classes is the wrong terminology – they should be called static nested classes instead
24
Creating objects of Inner Class public class A { public class Constants {... } public static void main(String[] args) { A a = new A(); A.Constants b = a.new Constants(); }
25
Copy Constructor public class SomeClass{ public Year getYear(){ return new Year(leapYear); } Vs public Year getYear(){ return leapYear; }
26
Abstract Classes Vs Interface Is a relationship Vs Contract Multiple Inheritance
27
Downcasting class Parent{ /*... */} class ChildClass extends Parent { /*... */ }... Parent p = new Parent ( ); ChildClass c = (ChildClass) p;...
28
final Modifier When applied to a Method: Method may not be overridden in a derived class. Class: Class can not be used as a base class to derive any class from it. Instance variable: Instance variable can’t be changed.
29
Serializable and transient variables public class SomeClass implements java.io.Serializable { // this variable will not persist private transient String password;... }
30
Anonymous Class HelloWorld frenchGreeting = new HelloWorld() { String name = "tout le monde"; public void greet() { greetSomeone("tout le monde"); } public void greetSomeone(String someone) { name = someone; System.out.println("Salut " + name); } }; Use once Inner clases
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.