Quiz 4 (20 pts)

  1. Given the code below, when you attempt to compile and run it, what will happen? (10 pts) (Select 1)
    //Q1.java
    public class Q1{
        private int a = 5;
        protected int b = 10;
        public int c = 15;
        int d = 20;
        public static void main(String[] args) {
            Q1 q = new Q1();
    1.      System.out.println(q.a);
    2.      System.out.println(q.b);
    3.      System.out.println(q.c);
    4.      System.out.println(q.d);
    	} 
    }
    
    a. compile time error, because line 1 tries to access a private variable
    b. compile time error, because line 2 tries to access a protected variable
    c. compile successfully, but get a runtime error
    d. compile and run it successfully. print 5 10 15 20
    
    
  2. Given the code below, when you attempt to compile it, which statement will give an error message?(10 pts)(Select 2)
    //Q2.java
    package javaone.quiz4;
    public class Q2{
        private int a = 5;
        protected int b = 10;
        public int c = 15;
        int d = 20;
    }
    
    //Q3.java
    import javaone.quiz4.Q2;
    public class Q3 extends Q2
        public static void main(String[] args) {
            Q3 q = new Q3();
    1.      System.out.println(q.a);
    2.      System.out.println(q.b);
    3.      System.out.println(q.c);
    4.      System.out.println(q.d);
    	} 
    }
    
    a. statement 1 tries to access a private variable
    b. statement 2 tries to access a protected variable
    c. statement 3 tries to access a public variable
    d. statement 4 tries to access a package variable