Question: Does each of the following programs compile and run? If no, explain the reason; otherwise, give the output. 1. class P1 { } }
Does each of the following programs compile and run? If no, explain the reason; otherwise, give the output. 1. class P1 { } } public static void main(String[] args) { A b = new B(); B a = b; a.show(); b.show(); class A { } } public String str="A"; public void show () { } class B extends A { System.out.println(str); public String str = "B"; public void show () { } System.out.println(str); 2. class P2 { } } public static void main(String[] args) { A o = new B(); o.m(); class A { } } char x = 'a'; public char getX() { return x; } public void m () { } class B extends A { char x = 'b'; System.out.println (getX()); public void m() { } System.out.println (getX()); 3. import java.util.*; class P3 { } ArrayList 1st; } public void add (Object obj) { if (!1st.contains (obj)) 1st.add(obj); } public static void main(String[] args) { P3 d = new P3(); d. add (1); d. add (1); d. add (2); d. add (2); 4. class P4 { } public static void main(String[] args) { try { } int value = 1; if (value 0.0 > 0) System.out.println("true"); } catch (Exception ex) { } System.out.println (ex.getMessage()); System.out.println("Continue after the catch block"); 5. import java.util.Scanner; class P5 { public static void main(String[] args) { Scanner input = new Scanner ("abc 123"); try { int i = input.nextInt (); } } } catch (Exception ex) { System.out.println("A"); } finally { } System.out.println("B");
Step by Step Solution
3.32 Rating (155 Votes )
There are 3 Steps involved in it
The images youve provided contain Java code snippets for different classes P1 P2 P3 P4 P5 with a question regarding their compilation and execution st... View full answer
Get step-by-step solutions from verified subject matter experts



![public static void main(String[] args) { A b = new B(); B](https://dsd5zvtm8ll6.cloudfront.net/questions/2024/01/65a668d2a6f56_1705494576870.jpg)
