18ai63 - Java For Mobile Applications Question Bank
18ai63 - Java For Mobile Applications Question Bank
A. Adding a contact
DBAdapter db = new DBAdapter(this);
// -- —add a contact--—
db.open();
long id = db.insertContact("Jennifer Ann", "[email protected]”) ;
id = db.insertContact("Oscar Diggs", "[email protected]") ;
db.close();
Retrieving a contact
// -- -get a contact---
db.open();
Cursor c = db.getContact(2);
if (c.moveToFirst())
DisplayContact(c);
else
Toast.makeText(this, “NO contact found”, Toast.LENGTH_LONG).show();
db.close();
Updating a contact
db.open();
if (db.updateContact(1,"Oscar Diggs', “[email protected]”))
Toast.makeText(this, "Update successful.”, Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Update failed.”, Toast.LENGTH_LONG).show();
db.close();
The deleteContact() method in the DBAdapter class deletes a contact using the
ID of the contact you want to delete. It returns a Boolean value, indicating
whether the deletion was successful.
4. Justify why Wrapper classes are required when compared to primitive types.
➔ Java uses primitive types (also called simple types), such as int or double, to
hold the basic data types supported by the language.
➔ Primitive types, rather than objects, are used for these quantities for the sake
of performance.
➔ Using objects for these values would add an unacceptable overhead to even the
simplest of calculations.
➔ Thus, the primitive types are not part of the object hierarchy, and they do not
inherit Object.
➔ Despite the performance benefit offered by the primitive types, there are times
when you will need an object representation.
➔ For example, you can’t pass a primitive type by reference to a method.
➔ Also, many of the standard data structures implemented by Java operate on
objects, which means that you can’t use these data structures to store primitive
types.
➔ To handle these (and other) situations, Java provides type wrappers, which are
classes that encapsulate a primitive type within an object.
➔ The type wrappers are Double, Float, Long, Integer, Short, Byte, Character, and
Boolean.
➔ These classes offer a wide array of methods that allow you to fully integrate the
primitive types into Java’s object hierarchy.
5. Explain with Java programs to demonstrate the use of values() and valueOf()
methods.
A.
You can construct a String object that contains the same character sequence as
another String object using this constructor:
String(String strObj)
Because 8-bit ASCII strings are common, the String class provides constructors
that initialise a string when given a byte array.
String(byte chars[], int startIndex, int numChars)
Here, chars specifies the array of bytes. The second form allows you to specify a
subrange. In each of these constructors, the byte-to-character conversion is
done by using the default character encoding of the platform.
10. Demonstrate the following string operations
i) String Literals
String literals in Java are specified like they are in most other languages—by
enclosing a sequence of characters between a pair of double quotes. Examples
of string literals are
The escape sequences and octal/hexadecimal notations that were defined for
character literals work the same way inside of string literals. One important
thing to note about Java strings is that they must begin and end on the same
line. There is no line-continuation escape sequence as there is in some other
languages.
In this case, age is an int rather than another String, but the output produced is
the same as before. This is because the int value in age is automatically
converted into its string representation within a String object. This string is
then concatenated as before. The compiler will convert an operand to its string
equivalent whenever the other operand of the + is an instance of String.
As you can see, Box’s toString() method is automatically invoked when a Box
object is used in a concatenation expression or in a call to println().
iii)getBytes()
iv) toCharArray()
12. How Strings can compared with following method?
i) equals and equalsIgnoreCase()
ii) regionMatches
ii) compareTo()
14. Explain indexOf() and lastIndexOf() methods in searching the strings
15. Explain the below methods with examples with respect to String Modification
i) substring()
ii) concat()
iii) replace()
iv)trim()