Exam AND-401 All QuestionsBrowse all questions from this exam
Question 99

Consider the following code snippet to query SQLite database:

String[] result_columns = new String[] {KEY_ID, COL1, COL2};

Cursor allRows = myDatabase.query(true, DATABASE_TABLE, result_columns, null, null, null, null, null, null);

Which of the following prints out the values of COL1 column correctly if the result is not empty?

    Correct Answer: A

    To print the values of the COL1 column correctly, we should retrieve the value at index 1 since the column array is defined as {KEY_ID, COL1, COL2}. Before accessing the data, we need to ensure that the Cursor moves to the first row. Therefore, using `if (allRows.moveToFirst()) { do { System.out.println(allRows.getString(1)); } while (allRows.moveToNext()); }` ensures that we start from the first row and iterate through all rows, printing the values of the COL1 column correctly.

Discussion
MmadiOption: A

A. if (allRows.moveToFirst()) { do { System.out.println(allRows.getString(1)); } while (allRows.moveToNext()); }