0% found this document useful (0 votes)
72 views

18ai63 - Java For Mobile Applications Question Bank

The document discusses various CRUD operations that can be performed on contacts in an Android application using a DBAdapter class. It demonstrates how to insert, retrieve, update, and delete contacts from a database through methods like insertContact(), getContact(), updateContact(), and deleteContact(). Wrapper classes in Java are discussed as well as how they allow primitive types to be fully integrated into the object hierarchy through methods like valueOf() and autoboxing/unboxing.

Uploaded by

Sahithi Bhashyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

18ai63 - Java For Mobile Applications Question Bank

The document discusses various CRUD operations that can be performed on contacts in an Android application using a DBAdapter class. It demonstrates how to insert, retrieve, update, and delete contacts from a database through methods like insertContact(), getContact(), updateContact(), and deleteContact(). Wrapper classes in Java are discussed as well as how they allow primitive types to be fully integrated into the object hierarchy through methods like valueOf() and autoboxing/unboxing.

Uploaded by

Sahithi Bhashyam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

18AI63 - JAVA FOR MOBILE APPLICATIONS QUESTION BANK

1. Demonstrate how CRUD operations can be performed programmatically in


Android application

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();

In this example, you create an instance of the DBAdapter class:


DBAdapter db = new DBAdapter(this) ;
The insertContact() method returns the ID of the inserted row. If an error
occurs during the operation, it returns —1.

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();

The getContact() method of the DBAdapter class retrieves a single contact


using its ID. You pass in the ID of the contact. In this case, you pass in an ID of 2
to indicate that you want to retrieve the second contact:
Cursor c = db.getContact(2);
The result is returned as a Cursor object. If a row is returned, you display the
details Of the contact using the DisplayContact() method. Otherwise, you
display a message using the Toast class.

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 updatecontact() method in the DBAdapter class updates a contact's details


by using the ID of the contact you want to update. It returns a Boolean value,
indicating whether the update was successful.
Deleting a contact
db.open();
if (db.deleteContact(1))
Toast.makeText(this,"Delete successful .” , Toast.LENGTH_LONG).show();
else
Toast.makeText(this, "Delete 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.

2. Explain Radio Button and Radio Button groups.


A.

3. What are enumerations? With an example code, demonstrate how varieties


of mangoes can be represented through enumeration.

A. In its simplest form, an enumeration is a list of named constants. By making


enumerations into classes, the capabilities of the enumeration are greatly
expanded. For example, in Java, an enumeration can have constructors,
methods, and instance variables. An enumeration is created using the enum
keyword.
0

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.

All enumerations automatically contain two predefined methods: values() and


valueOf(). Their general forms are shown here:
public static enum-type [] values()
public static enum-type valueOf(String str)
The values() method returns an array that contains a list of the enumeration constants.
The valueOf() method returns the enumeration constant whose value corresponds to
the string passed in str.
6. Demonstrate with a Java code, how Auto boxing/Unboxing Occurs in
Expressions?
A. In general, autoboxing and unboxing take place whenever a conversion into an
object or from an object is required. This applies to expressions. Within an expression,
a numeric object is automatically unboxed. The outcome of the expression is reboxed,
if necessary.

7. How default values can be used in an Annotations? Explain with an example


Java Code.
A. You can give annotation members default values that will be used if no value is
specified when the annotation is applied. A default value is specified by adding a
default clause to a member’s declaration. It has this general form:
type member() default value;
Here, value must be of a type compatible with type.
8. How Auto boxing/Unboxing can be used to prevent errors?
A.
This program displays not the expected value of 1000, but –24! The reason is
that the value inside iOb is manually unboxed by calling byteValue( ), which
causes the truncation of the value stored in iOb, which is 1,000. This results in
the garbage value of –24 being assigned to i. Auto-unboxing prevents this type
of error because the value in iOb will always auto-unbox into a value
compatible with int.

9. Write a Java program to instantiate different Constructors supported by


String class.
A. The String class supports several constructors. To create an empty String,
call the default constructor. For example, String s = new String(); will create
an instance of String with no characters in it.
To create a String initialised by an array of characters, use the constructor
shown here:
String(char chars[])

You can specify a subrange of a character array as an initializer using the


following constructor:
String(char chars[], int startIndex, int numChars) Here, startIndex specifies
the index at which the subrange begins, and numChars specifies the number of
characters to use.

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.

ii) String Concatenation


In general, Java does not allow operators to be applied to String objects. The
one exception to this rule is the + operator, which concatenates two strings,
producing a String object as the result. This allows you to chain together a
series of + operations. For example, the following fragment concatenates three
strings:

iii) String Concatenation with other data types

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.

iv) String Conversion and toString()


When Java converts data into its string representation during concatenation, it
does so by calling one of the overloaded versions of the string conversion
method valueOf() defined by String.valueOf() is overloaded for all the
primitive types and for type Object. For the primitive types, valueOf() returns a
string that contains the human-readable equivalent of the value with which it is
called. For objects, valueOf() calls the toString() method on the object.
Every class implements toString() because it is defined by Object. However,
the default implementation of toString() is seldom sufficient. For most
important classes that you create, you will want to override toString() and
provide your own string representations.
By overriding toString() for classes that you create, you allow them to be fully
integrated into Java’s programming environment. For example, they can be
used in print() and println() statements and in concatenation expressions.
The following program demonstrates this by overriding toString() for the Box
class:

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().

11. How following methods can be used in character extraction?


i) charAt()
ii) getchars()

iii)getBytes()

iv) toCharArray()
12. How Strings can compared with following method?
i) equals and equalsIgnoreCase()

ii) regionMatches

iii) startsWith and endsWith()


13. Explain the following methods with examples.
i) equals() and ==

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()

You might also like