Points To Remember for SCJP

Similar documents
AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

1 Shyam sir JAVA Notes

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

Operators and Expressions

15CS45 : OBJECT ORIENTED CONCEPTS

Index COPYRIGHTED MATERIAL

Introduction to Programming Using Java (98-388)

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Declarations and Access Control SCJP tips

Language Features. 1. The primitive types int, double, and boolean are part of the AP

Java Overview An introduction to the Java Programming Language

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

CSC Java Programming, Fall Java Data Types and Control Constructs

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

3. Java - Language Constructs I

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

1 Epic Test Review 2 Epic Test Review 3 Epic Test Review 4. Epic Test Review 5 Epic Test Review 6 Epic Test Review 7 Epic Test Review 8

c) And last but not least, there are javadoc comments. See Weiss.

Java Primer 1: Types, Classes and Operators

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Class, Variable, Constructor, Object, Method Questions

CSC 1214: Object-Oriented Programming

CS260 Intro to Java & Android 03.Java Language Basics

Modern Programming Languages. Lecture Java Programming Language. An Introduction

9 Working with the Java Class Library

A Short Summary of Javali

Introduction. Assessment Test. Part I The Programmer s Exam 1

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Client-Side Web Technologies. JavaScript Part I

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Brief Summary of Java

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Some Interview Question-Answers on Java JAVA. dfghjklzxcvbnmqwertyuiopasdfghjklzx 1/10/2013. cvbnmqwertyuiopasdfghjklzxcvbnmq.

Compaq Interview Questions And Answers

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Java Fundamentals (II)

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

Java 2 Programmer Exam Cram 2

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Core Java Interview Questions and Answers.

Types, Values and Variables (Chapter 4, JLS)

Casting -Allows a narrowing assignment by asking the Java compiler to "trust us"

COP 3330 Final Exam Review

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Program Fundamentals

IC Language Specification

Chapter 2: Using Data

5/23/2015. Core Java Syllabus. VikRam ShaRma

WA1278 Introduction to Java Using Eclipse

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

Java Threads and intrinsic locks

Full file at

Selected Java Topics

CMSC 132: Object-Oriented Programming II

Java Programming. Atul Prakash

(Not Quite) Minijava

VARIABLES AND TYPES CITS1001

Rules and syntax for inheritance. The boring stuff

3. Convert 2E from hexadecimal to decimal. 4. Convert from binary to hexadecimal

Training topic: OCPJP (Oracle certified professional Java programmer) or SCJP (Sun certified Java programmer) Content and Objectives

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Chapter 3: Operators, Expressions and Type Conversion

Computer Components. Software{ User Programs. Operating System. Hardware

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

JAVA Programming Fundamentals

Language Fundamentals Summary

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Java Programming Training for Experienced Programmers (5 Days)

Full file at Chapter 2 - Inheritance and Exception Handling

public class Test { static int age; public static void main (String args []) { age = age + 1; System.out.println("The age is " + age); }

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

3. Java - Language Constructs I

JAVA MOCK TEST JAVA MOCK TEST III

PESIT Bangalore South Campus

Week 6: Review. Java is Case Sensitive

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

JOSE LUIS JUAREZ VIVEROS com) has a. non-transferable license to use this Student Guide

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

Study Notes for Sun Certified Programmer for Java 2 Platform

Java Programming. MSc Induction Tutorials Stefan Stafrace PhD Student Department of Computing

CS Internet programming Unit- I Part - A 1 Define Java. 2. What is a Class? 3. What is an Object? 4. What is an Instance?

Chapter 2: Using Data

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Definition of DJ (Diminished Java)

Mobile MOUSe JAVA2 FOR PROGRAMMERS ONLINE COURSE OUTLINE

CMSC 132: Object-Oriented Programming II

1 OBJECT-ORIENTED PROGRAMMING 1

Full file at

Values and Variables 1 / 30


Objective Questions. BCA Part III Paper XIX (Java Programming) page 1 of 5

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Syllabus & Curriculum for Certificate Course in Java. CALL: , for Queries

Transcription:

Points To Remember for SCJP www.techfaq360.com The datatype in a switch statement must be convertible to int, i.e., only byte, short, char and int can be used in a switch statement, and the range of the datatype used must cover all of the cases (case statements). An integer literal (w/o a decimal pt) is assumed to be a 32-bit int primitive, but a value containing a decimal point is assumed to be a 64-bit double. Wrapper classes for primitives produce immutable objects. Java characters are 16-bit unicode characters, and they can be initialized in the following ways: char c1 = '\u0057' char c1 = 'w' char c1 = 87 char c1 = (char) 87 (acc to RHE) char c1 = '\r' instanceof operator can take a class, an interface or an array type as an argument. To test if an object is an array, use: myobject.getclass().isarray(); MenuContainer interface is implemented by Container, Frame, Menu and MenuBar An identifier must begin with a letter, a dollar sign ($), or an underscore. Subsequent characters maybe letters, $, underscore, or digits, but NOT other characters liek %, &, etc. The keywords syncrhonized, private and protected cannot be used with a class. A variable declared inside a try block cannot be used in the catch block. If no super-class constructor is explicitly called, and there is no call to other constructor (using this(..)), the default constructor of the superclass (w/o any arguments) is automatically called. wait(), notify() and notifyall() are the instance methods of Object class, not Thread class (but they are inherited by Thread class). stop(), suspend() and resume() are deprecated methods in Java 1.2 References to member methods are resolved at runtime using the type of object, but the references to member variables are computed at compile time using the type of the reference. Javadoc documentation does not list private variables and methods. A valid overload differs in teh number or types of arguments. Difference is return type only is not a valid overload. Inner classes defined local to a block may not be declared to be static. Mmember inner classes cannot have the same name as enclosing class, but they CAN subclass the enclosing class. If an instance of the inner class had to refer to the associated instance of the enclosing class, then the following syntax could be used: EnclosingClass mypart = EnclosingClass.this; No datatype is automatically converted to a char. However, you can cast to a char. Solaris uses pre-emptive scheduling; Windows & Mac uses time-slicing. A monitor is any object that has some synchronized code. If you have a sycnhronized method in a class, a subclass with an overriding method does not

have to declare it to be synchronized. A static method may not be overridden to be non-static, and vice-versa. equals() method just returns false even if the object reference types are different. It does not throw an exception. The add(..) method of the Set returns false if you atttempt to add an element with a duplicate value, but no exception is thrown. Constructors CAN be private. Anonymous classes cannot have constructors. -(i) = ~(i) + 1 The test condition (assuming i is an int): If (i=2) will give a compiler error because the test condition expects a boolean and the assignment (i=2) returns an int While using the RandomAccessFile constructor, if the file is not present, a mode "r" throws a FileNotFoundException. If "rw" mode is used, a new file is created with zero length. Applets can't have a constructor with arguments. It can have constructor with no arguments. Any component can have menu barsor pull-down menus, although only frames have a method for adding a menubar. Double.NaN == Double.NaN //returns false But, if Y = X = Double.NaN, X.equals (Y) returns true Also, +0.0 == -0.0 //returns true But, if Y = +0.0 and X = -0.0, X.equals (Y) returns false For shift operators, >>, << and >>>, unary numeric promotion is applied to each operand separately, and the datatype of the resulting expression is the same as the left operand. instanceof is not same as instanceof. The instanceof operator tests whether its first operand is an instance of its second. op1 instanceof op2 op1 must be the name of an object and op2 must be the name of a class. An object is considered to be an instance of a class if that object directly or indirectly descends from that class. There is nothing like instanceof in java x instanceof P If x is null, the instanceof test simply returns FALSE --it doesn't cause an exception String[] str = new String[10]; now str instanceof String[] ===returns true str instanceof Object ===returns true str instanceof Object[] ===returns true str instanceof String ===Compilation Error If a = null, System.out.println (a); prints null, and does not throw an exception. A class SHOULD (MUST) be declared abstract if it has one or more abstract methods. An abstract class can also have non-abstract methods. You can also declare a class to be abstract even when it has no abstract methods, so that users can't instantiate it and are forced to subclass

it. In an interface, ALL methods must be abstract. Infact, all methods in an interface are implicitly abstract. Also, all methods and variables in an interface are implicitly public, and must declare them to be public while implementing the interface. It is a compile-time error for a constructor to invoke itself by calling this(), but there is a bug which allows cyclic constructor invocation, that is constructor 1 calls constructor 2 and vice-versa. If you call Thread.sleep() in a synchronized code, the thread does not give up the lock, it keeps the lock during sleeping. It gives up the lock only when calling the method wait(). abs (Integer.MIN_VALUE) = Integer.MIN_VALUE No duplicate objects are allowed in a Set, and duplicate means that the equals() method returns true. So, there can't be two separate objects having same value in a set. If run() is a static method, and me() is also a static method returning null, then me().run() WILL compile and run correctly. In other words, ((StaticTest)(null)).run(); //will work fine (where StaticTest is a class) Byte b1 = new Byte("172") b1.tostring() == b1.tostring() is false. The tostring() method for the Wrapper classes (except Boolean) creates a string over the heap, and returns a reference to that string. A new string is created eacht time, hence the result false. Each instance of a thread belongs in a ThreadGroup (even if you don't explicitly set it). In the latter case, the threadgroup is the same that of the thread which created it. If i=0 i++ will result in i=1 i=i++ will not change the value of i. It'll remain at 0. You can use super.var to access a variable in the superclass. Even new Boolean (null); will create a Boolean with a false value, and won't throw an exception. This is true although null is a null literal, and not the same as "null" string. Interfaces can have access modifiers of public or blank, just like classes. UTF-8 is ASCII An array of primitives of on type canot be cast into an array of primitives of another type. The \uxxxx notation can be used anywhere in the source to represent Unicode characters, e.g., char a = '\u0061' ch\u0061r a = 'a char \u0061 = 'a' are all valid (and equivalent) statements. An empty file is a valid source file. A source file can contain an optional package declaration, any number of import statements and any number of classes and interface definitions. The modulus operator (%) can be used with integral as well as floating datatypes. Extended assignment operators, e.g., *=, += ensure than an implicit narrowing conversion takes place which makes the result fit into the target variable, e.g., b = b + i; // is illegal. b+=i //is legal In an assignment of the following type, where i=0:

a[i] = i = 9; a[0] will be assigned the value of i=9, i.e., 9, as opposed to a[9], which you might think. That is, the initial value is used to determine which element is to be assigned the value. Elements in an unitialized array object get the default value corresponding to the type of elements. This is true, regardless of whether the variable is an instance variable or a local variable. Local variables cannot be declared to be static and cannot be given an accessibility modifier. The declaration of a non-abstract method must provide an implementation ( { }, at least!! ). A constructor does not declare any return type, not even a void. A constructor cannot be final, abstract or static. Normal methods (non-constructor) MUST specify a return type, at least a void!! You cannot pass void as a parameter to a method, e.g., void method (void) is wrong. Anonymous arrays, (analogous to anonymous classes) have a form: new int[] {3,2,4,6,1} and are usually used for being passed to a method as a parameter. The main() method CAN throw checked exceptions. It is not possible to use a break statement inside an if block. Break statements can only be used for o Do-while o While o For o Labelled blocks o Switch statements catch and finally blocks can themselves throw checked exceptions which are handled in the usual way. An overriding method must have the same method name, same parameters, and same return type as the original method. Whether parameters in the overriding method should be declared final is at the discretion of the subclass. Only methods that are accessible may be overridden. So, private methods can't be overridden. this() and super() cannot both occur in the same constructor. If a constructor does not have either a this() or a super() as its first statement, then a super() call to the default constructor of the superclass is automatically inserted. An interface can define constants. Such constants are considered to be public, static and final regardless of whether these modifiers are specified or not. In the case of multiple inheritance, any name conflicts are resolved using fully qualified names. A non-static inner class cannot have any static members. Package member classes, local classes and anonymous classes cannot be declared to be static. The static initializer block cannot be contained in a method. In a method declaration, the modifiers, e.g., public, static, must precede the return type. All method defined in Set are also defined in Collection, but the same is not true for List. Java returns the same string object reference for almost all of the methods invoked on a string

object, provided the result of the operation happened to be the same as that of the string object on which the method is invoked. According to the preceding point, a comparison like this: "abc".substring(0)=="abc" would return true. For the wrapper classes, a new string reference is created each time, except for Boolean class, which returns a reference to "true" or "false" from the string pool. String comparisons for strings created at runtime, return false, e.g., if ja="ja" and va = "va" and java="java" java == "java" //is true java = "ja" + "va" //is true java = "ja" + va //is false A vector can only store object references, NOT primitives. length is a field (instance variable) for an array, but a string length() is a method. Math.round (-4.7) = -5 Math.ceil (-4.7) = -4.0 Math.floor (-4.7) = -5.0 Native methods cannot be abstract, but they can be static. Garbage collection cannot be turned off. double c = Math.floor (Double.MIN_VALUE) // = 0.0 double c = Math.ceil (Double.MIN_VALUE) // = 1.0 For Math.round() o Add 0.5 to the number to be rounded. o Do a Math.floor() on that number. getvalue() method of the Adjustment class returns an int value for the adjustment setting. When a thread executes a waitforid() call on a MediaTracker, the current thread stops executing. The finally block is always executed except when there is a System.exit call. Local inner classes cannot be declared public, private, protected, or static. Member inner classes can be private, protected, public, final or abstract. Component class is an abstract class. Vector v = new Vector (initial capacity, capacity increment), e.g., Vector v = new Vector (5, 10), where 5 is the initial capacity of the vector and 10 is the capacity increment. There is no concat() method for StringBuffer class. replace() is a String method. The read() method returns -1 when it reaches end of the file. Using the modifier static or final with transient is legal. An abstract method cannot be private, static, final, native or synchronized. Object method equals() will throw a NullPointerException if the calling object is null. If the argument is null, it returns false. If a method is there in a subclass, but not in the base class, then you can't use a reference variable of the base type to access that method, even if the object is of the subclass type. You'll be

required to cast the reference to subclass type. getwhen() method of InputEvent class is used to extract the instant at which the InputEvent (e.g., MouseEvent) was generated. Boolean and Character classes do no extend java.lang.number. Character class does not have a valueof() mehod. add() and additem() both can be used to add an item to a choice. Math.ceil (-0.5) = -0.0 ( not 0.0!!) The order of the floating/double values is: -infinity -> -ve nos/fractions -> -0.0 -> 0.0 -> +0.0 -> +ve nos/fractions -> +infinity 2 + 3 + "" = 5 2 + "" + 3 = 23 " + 2 + 3 = 23 A label CAN be placed in front of another label. You can't assign a 2nd value to a final variable. You CAN assign the value like this: final int MAX_INT; MAX_INT = 30; but you cannot reassign the value. null, true and false, are NOT keywords. They are literals. null is a NULL literal. True and false are boolean literals. But remember that they ARE reserved words. You can put a semicolon after '}' If i = 3, then print3 (i, ++i, ++i +++i) is equivalent to print3 (3, 4, 5+6) print3 (i, ++i, i+++ i ++) is equivalent to print3 (3, 4, 4+5) Once an array is created, its length can never be changed. The array-size specifier must be an integral type, you can't use long, etc. StringBuffer class does not ovverride equals method. So, the equals method returns false when passed two different StringBuffer objects, containing the same values. float f = 40.0 // illegal because 40.0 is double byte b = 40 // legal although 40 is int int arr[] = new int [-10] compiles correctly, but throws a NegativeArraySizeException at runtime. Static variables CAN exist inside an inner class if the inner class is also static. square() or sqr() methods do not exist for Math class, because same functionality can be provided by pow(). An inner class can actually be a subclass of the outer class. Runtime exceptions can be avoided with a correctly coded program. However, checked exceptions can arise even in a correctly coded program. The implementation of tostring() method for the String class simply returns a reference to the current object, i.e., String s1 = new String("Java"); String s2 = s1.tostring(); s1 == s2 // is true

abs, min, max and round are the only methods (out of the methods which you need to remember for SCJP ;-) ) of the Math class with return an int (also). There is a static method for Integer class: public static String tostring(int I) which can be used to return a String object representing the specified integer. There are similar methods for Long, Float, Double, Byte, Short. Overriding methods cannot throw checked exceptions not thrown by overridden methods, but they CAN throw unchecked exceptions not thrown by overridden methods, like ArrayIndexOutofBoundsException. Unary numeric promotion is not applied to ++ and You can also run a class from the command line, even if it is not declared public. protected means accessible by all the subclass, as well as all the classes in the same package. When -ve signs are involved in % operator, compute the result by ignoring the sign(s) and then, just apply the sign of the left operand to the result, e.g., -7 % -2 = - (7%2) = - 1 instanceof operator simply returns false if the left hand argument is a null value. It does not throw an exception. The default no-argument constructor created by Java is public. When asked to write a statement, be sure to end it with a semicolon. The order of the exceptions specified by a method should be more specific exception followed by less specific exception. A try block must be followed by one of these combinations: o One or more catch blocks. o A finally block. o Both catch and finally blocks. The main method can be declred without the public modifier, and it'll run fine. A more specific catch block cannot follow a general one. The program won't compile. If you have two methods in a class: o public int method (int b, float x) o public int method (float b, int x) and you call one of these methods as method (2, 5), the program won't compile, because the reference to the method is ambigous. However, if you call the method as method (2, 5.0), it'll compile and run fine. reverse() is a method of StringBuffer class, not String class. In case of shift-operators, only primitive integral types (byte, short, char, int, and long) can be used, and unary numeric promotion is applied separately to each operand.