📜 ⬆️ ⬇️

Java for Professionals: Specification Tasks

In preparation for the SCJP certification exam, as well as when reading in various sources the nuances of using the Java language, many different questions arise. Also, some features of the language seem completely illogical, but this is indicated in the language specification, and every self-respecting professional seeks to know these little things.

This topic is devoted to short examples of programs (or code sections). Each program needs to answer simple questions: how it will be executed and what will be displayed on the console after its execution. It is assumed to use Java 1.6, unless otherwise specified in the task.


')
PS If you have interesting tasks on the same topic - lay out in the comments.

I bring to your attention the first portion of tasks.

  1. What numerical constant needs to be substituted for ellipsis to make the expression true? (answers are also accepted in the form of standard constants declared in wrapper classes over primitive types)

     (int) ((char) -1) == ...
    
     (short) ((int) ((char) -1)) == ...
    

  2. What constant should be substituted for ellipsis in order to display COOL?

     int x = ...;
    
     if (x! = 0 && x == -x) {
    	 System.out.println ("COOL");
     }
    


    What will change if you change the type to long?
  3. What constant should be substituted for ellipsis in order to display COOL?

     double x = ...;
    
     if (x! = x) {
    	 System.out.println ("COOL");
     }
    


  4. What will be derived from the execution of this program?

     public class Test {
    	 static int a () {print ("a");  return 1;  }
    	 static int b () {print ("b");  return 1;  }
    	 static int c () {print ("c");  return 1;  }
    
    	 static void print (String s) {System.out.println (s);  }
    
    	 public static void main (String [] p) {
    		 int d = a () + b () * c ();
    		 print ("" + d);
    	 }
     }
    

  5. What will be derived from the execution of this program?

     public class Test {
    	 static int a () {print ("a");  return 1;  }
    	 static int b () {print ("b");  return 1;  }
    	 static int c () {print ("c");  return 1;  }
    
    	 static void print (String s) {System.out.println (s);  }
    
    	 public static void main (String [] p) {
    		 if (a () == b () || a () == c ()) {
    			 print ("d");
    		 }
    	 }
     }
    

  6. What will be derived from the execution of this program?

     public class Test {
    	 static int a () {print ("a");  return 1;  }
    	 static int b () {print ("b");  return 1;  }
    	 static int c () {print ("c");  return 1;  }
    
    	 static void print (String s) {System.out.println (s);  }
    
    	 public static void main (String [] p) {
    		 try {
    			 if ((a () + b ()) / (a ​​() - b ()) + c () == a ()) {
    				 print ("d");
    			 }
    		 } catch (Throwable e) {
    		 }
    	 }
     }
    

  7. What will be derived from the execution of this program?

     class BasicExcept extends RuntimeException {
       private int b = 10;
    
       public BasicExcept () {
         b = 15;
         throw this;
       }
    
       {
         b = 5;
       }
    
       public int getB () {
         return b;
       }
    
       public String toString () {
         return "Superclass";
       }
     }
    
     class Except extends BasicExcept {
       private int a = 10;
    
       public Except () {
         super ();
         a = 15;
       }
    
       {
         a = 5;
       }
    
       public int getA () {
         return a;
       }
    
       public String toString () {
         return "Subclass";
       }
     }
    
    
     public class Test {
       public static void main (String [] args) {
         try {
           new Except ();
           System.out.println ("No exception");
         } catch (Except e) {
           System.out.println ("Exception occured! A =" + e.getA ()
               + ", B =" + e.getB ()
               + ", toString () =" + e.toString ());
         }
       }
     }
    

  8. What will be derived from the execution of this program?

     class TryExcept extends RuntimeException {
         private String s;
    
         static {
             init ();
         }
    
         public TryExcept () {
             s = "hahaha";
         }
    
         private static void init () {
             throw new TryExcept ();
         }
    
         public String get () {
             return s;
         }
     }
    
     public class Test {
         public static void main (String [] args)
         {
             for (int i = 0; i <2; i ++) {
                 try {
                     new TryExcept ();
                 } catch (Throwable e) {
                     System.out.println ("e =" + e);
    
                     if (e.getCause () instanceof TryExcept) {
                         TryExcept ex = (TryExcept) e.getCause ();
                         System.out.println ("e instanceof"
                             + TryExcept.class.getName ()
                             + ", s =" + ex.get ());
                     }
                 }
             }
         }
     }
    



Thanks to those who helped find and correct errors in the topic:
barker , mohax , sanix

Source: https://habr.com/ru/post/89580/


All Articles